Python Newbie
Roy Smith
roy at panix.com
Sun Feb 24 11:22:36 EST 2013
In article <mailman.2410.1361721167.2939.python-list at python.org>,
Chris Angelico <rosuav at gmail.com> wrote:
> The "dunder" methods ("d"ouble "under"score, leading and trailing),
> also called "magic methods", are the implementations of various
> special features. For instance, indexing foo[1] is implemented using
> the __getitem__ method. Here's a list:
>
> http://docs.python.org/3.3/reference/datamodel.html#special-method-names
>
> You'll seldom, if ever, call these methods directly.
On the other hand, once you get into building your own classes, you will
often be *writing* them. The most common are __str__(), __repr__(), and
__unicode__(), and of course, __init__().
A quick look over my current project shows 471 classes, and I've defined:
1 __del__
1 __getattr__
1 __iter__
1 __new__
2 __cmp__
2 __len__
3 __ne__
4 __contains__
9 __eq__
14 __str__
38 __unicode__
62 __repr__
140 __init__
Not to mention the boilerplate:
if __name__ == '__main__":
which shows up all over the place.
More information about the Python-list
mailing list