0
Im sorry for asking this bit what are these magic methods for? (except __init__)
3 Respuestas
+ 4
they are useful to make operator overloading for example. A example let say you have two objects from a class Product, let's call then Pen and Pencil. these two objects have an attribute called price and you want to sum the price of them. so you could create a method sum with magic method __add__ to calculate Pen.price + Pencil.price. After you have done this method , you will be able to compute price of whatever object from class Product you want. I hope I was clear enough :)
0
they are implicitly called in certain situations like using operators, type conversions, etc. you can read more here http://www.rafekettler.com/magicmethods.html .
0
thank you