+ 2
I am completely lost on magic-methods
Pls I am completely lost on magic-methods, anyone with a better explanation?
3 Réponses
+ 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
+ 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.
- 1
Totalement perdu :(