Special Functions
This are also known as dunder (double underscore) or magic functions. They are signified by two underscores before and after its name i.e. __len__.
These functions are not normally called by the user, the are for the python interpreter to use. For example if you call
item in some_iterable:
pass
to get the list of items this calls some_iterable.__iter__()
. The same for len(object)
this calls object.__len__()
.
When you are writing an object in python it is good to define this if they are not inherited from the parent class. This will guarantee pythonic behaviour.