Pages

Saturday, January 1, 2011

Special Methods


There are certain special methods which have special significance in classes such as the __init__ and
__del__ methods whose significance we have already seen.
Generally, special methods are used to mimic certain behavior. For example, if you want to use the
x[key] indexing operation for your class (just like you use for lists and tuples) then just implement the
__getitem__() method and your job is done. If you think about it, this is what Python does for the
list class itself!
Some useful special methods are listed in the following table. If you want to know about all the special
methods, then a huge list is available in the Python Reference Manual.

No comments:

Post a Comment