Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
you use magic methods within classes. They allow you to use different operations on your class. the __init__ magic method, for example, is called when a class is INSTANTIATED and lets you pass variables into your class (essentially, its the same as a constructor in other languages). the __mul__ method lets you multiply an instance of the class by another object. eg. class A: def __mul__(self,x) return 2*x a = A() print(a*7) #outputs 14 the __repr__ method is used to represent your class as a string, when using print for example I hope this sheds some light on the matter. If you want to see a real working example of this, check out my Vector2D code
8th Feb 2017, 1:23 PM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar