class magic methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answer
+ 3
Here some explanation. Check out the comments they are often very helpful. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2470/?ref=app 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
Stefanoo - avatar