Custom Search

Friday, April 2, 2010

Special Methods in python

Special Methods in python

Special methods (http://docs.python.org/ref/specialnames.html) start and
end with a double underscore, and no normal method should use this convention.
They are used for operator overloading, container definitions, and so on. For the sake
of readability, they should be gathered at the beginning of class definitions:

>>> class weirdint(int):
... def __add__(self, other):
... return int.__add__(self, other) + 1
... def __repr__(self):
... return '' % self
... #
... # public API
... #
... def do_this(self):
... print 'this'
... def do_that(self):
... print 'that'

For a normal method, you should never use these kinds of names.

---------------------

No comments:

Post a Comment