I am completely lost on magic-methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I am completely lost on magic-methods

Pls I am completely lost on magic-methods, anyone with a better explanation?

28th Oct 2018, 4:14 PM
Uchenna Obere
Uchenna Obere - avatar
3 Answers
+ 7
Magic methods are most frequently used to define overloaded behaviours of predefined operators in Python. For instance, arithmetic operators by default operate upon numeric operands. This means that numeric objects must be used along with operators like +, -, *, /, etc. The + operator is also defined as a concatenation operator in string, list and tuple classes. We can say that the + operator is overloaded. In order to make the overloaded behaviour available in your own custom class, the corresponding magic method should be overridden. For example, in order to use the + operator with objects of a user-defined class, it should include the __add__() method
28th Mar 2020, 4:16 AM
Zahid Zubair
Zahid Zubair - avatar
+ 4
You can dig some more info in here: https://www.sololearn.com/discuss/290594/?ref=app https://www.sololearn.com/discuss/1439914/?ref=app https://www.sololearn.com/discuss/1165326/?ref=app Basically those are special-purpose methods responsible for the most basic Python features and actions.
28th Oct 2018, 6:01 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
- 1
Totalement perdu :(
22nd Nov 2020, 8:02 AM
Ali