First page Back Continue Last page Overview Graphics
Parameter Overloading
The following example shows polymorphism as operator overloading with the „+“-Operator
>>> a = "Spam spam spam"
>>> b = a + "!"
>>> 3 + 4
7
Although we can implement all class behaviour as method functions, it is in many cases more convenient to use operator overloading.
- Python provides specially named methods __x__ to carry out operator overloading, e.g. __add__ for +
- These methods are automatically called if the corresponding operator is found in an expression
- overloaded operators are like built-ins
- most built-in operators can be overwritten
a + b
__add__(self,other)