What is self?
Magic methods let you define how objects of your class behave with Python's built-in operations. Some important ones include:
__str__ and __repr__ - Control how your object is printed
__str__
__repr__
__len__ - Define what len(object) returns
__len__
len(object)
__add__, __sub__, etc. - Enable math operators like + and -
__add__
__sub__
+
-
__eq__, __lt__, etc. - Enable comparison operators
__eq__
__lt__
__getitem__ and __setitem__ - Make your object indexable like a list
__getitem__
__setitem__
__call__ - Make your object callable like a function
__call__
Last updated 3 months ago