Python Classes - Operator Overloading Methods with Code Example - Learn to Program APPFICIAL

Опубликовано: 10 Ноябрь 2021
на канале: Appficial
1,171
26

Operator Overloading allows you to replace the functionality of numeric operators (such as +, -, *, and /) with a method

For example, you can write an __add__(self, other) method to replace the functionality of the (+) operator

Use the built-in isinstance() method to handle different object types for the operation

Method / Description
__add__(self, other) / Add (+)
__sub__(self, other) / Subtract (-)
__mul__(self, other) / Multiply (*)
__truediv__(self, other) / Divide (/)
__floordiv__(self, other) / Floored division (//)
__mod__(self, other) / Modulus (%)
__pow__(self, other) / Exponentiation (**)
__and__(self, other) / "and" logical operator
__or__(self, other) / "or" logical operator
__abs__(self) / Absolute value (abs())
__int__(self) / Convert to integer (int())
__float__(self) / Convert to floating point (float())

Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!


На этой странице сайта вы можете посмотреть видео онлайн Python Classes - Operator Overloading Methods with Code Example - Learn to Program APPFICIAL длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Appficial 10 Ноябрь 2021, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 1,171 раз и оно понравилось 26 зрителям. Приятного просмотра!