__init__
method. This
method is called whenever the class name is used as a constructor, and allows you to
initialize attributes in your object at the same time as you create it. The
__str__
method is called through the print statement; the __repr__
method is called when an object's name is typed in the interpreter. Table 10.1
lists some of the more commonly used methods for overloading.
In addition, you can define what happens when your object is iterated over by means of
the for statement by defining an __iter__
method that simply returns
the object itself, and providing a next method which will be called for
each iteration. Inside the next method, you need to raise a
StopIteration exception when no more items are available. (See Section 10.10
for an example.