Lecture_5#_Objects_as_attributes (1)
Lecture_5#_Objects_as_attributes (1)
Using
Python
Teemu Matilainen
teemu.matilainen@savo
nia.fi
Lecture 5#
Function Description
Reference: https://www.programiz.com/python-programming/operator-overloading
Addition p1 + p2 p1.__add__(p2)
Subtractionp1 - p2 p1.__sub__(p2)
Multiplication p1 * p2 p1.__mul__(p2)
Power p1 ** p2 p1.__pow__(p2)
Division p1 / p2 p1.__truediv__(p2)
Floor Division p1 // p2 p1.__floordiv__(p2)
Remainder (modulo) p1 % p2 p1.__mod__(p2)
Bitwise Left Shift p1 << p2 p1.__lshift__(p2)
Bitwise Right Shift p1 >> p2 p1.__rshift__(p2)
Bitwise AND p1 & p2 p1.__and__(p2)
Bitwise OR p1 | p2 p1.__or__(p2)
Bitwise XOR p1 ^ p2 p1.__xor__(p2)
Bitwise NOT ~p1 p1.__invert__()
Python Special Functions…
Reference: https://www.programiz.com/python-programming/operator-overloading
Let’s add
__eq__ to
Engine class.
One more
operator
example…
What is a
__call__
operator?
What is a
__call__
operator?
Ensures that instances of a class behave consistently with both built-in types and
other user-defined types, promoting a uniform and predictable experience.
Streamlines code writing, particularly for intricate data types, making the
codebase more straightforward and easier to comprehend.
Code Reusability:
A list of
objects as
an attribute
of an
object???
- Instance method
- Static methods
- …and also, Dynamic method
Copying an object…