class magic methods | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

class magic methods

How do these methods work, mostly __and__, __xor__ and __or__? how is class necessary in a code when defined functions can also carry out the same job?

12th Oct 2021, 1:33 AM
Demiz 🇬🇭🇬🇭🇱🇷🇱🇷
Demiz 🇬🇭🇬🇭🇱🇷🇱🇷 - avatar
1 ответ
+ 3
Here some explanation. Check out the comments they are often very helpful. If you write a class and implements a magic method like __add__ you can decide what should happen if use the + sign with your class. For example you have a class Account and you want add two accounts you can now implement this method and can now use the + sign. This can be very useful if you write tests and all classes have the same design.
12th Oct 2021, 2:25 AM
Stefanoo - avatar