Special Class Methods (besides __init__)

__add__
__sub__
__mul__
__truediv__
__pow__
__neg__
__repr__
__radd__
__rsub__
__rmul__
__rtruediv__
__call__

Say you have a class c, and two variable of type c (let’s call them a and b) if you do the following, it won’t really have any desired result.

class c:
    def __init__(self):
	    ...
    ...

a = c()
b = c()
a + b # error!
a - b # error!
a * b # error!
a / b # error!
a ** b # error!
-a # error!
a + 2 # error!
6 * b # error!
print(a) # doesn't really print anything useful

These special class methods tell Python what to do when you do the operations above.

The full list of special class methods

More about OOP: OOP Things

#python #python/features