Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Arithmetical and logical operations

In Python, when we add two numbers, what is happening under the hood is that one of them is calling its method on the other.

Consider the following example:

>>> 1 + 23

Here, under the hood, 1 is calling its __add__ method on 2. Inside, 1 is checking whether 2 is of a supported type, and runs the computation.

We don't want to mess with number behavior, but this process is exactly the same for any custom class as well. In other words, if you define the __add__ function of your class, you'll be able to add something to this class. In some cases, you might try to add something that your class doesn't know how to handle. It should then raise a built-in notImplemented exception. In this case, Python will try ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content