double underscore attributes?

bobueland at bobueland at
Sat Dec 10 21:17:45 CET 2005


I get
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__',
'__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
'__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__',
'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
'__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
'__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
'__truediv__', '__xor__']

Every time I use dir(some module) I get a lot of attributes with double
underscore, for example __add__. Ok, I thought __add__ must be a method
which I can apply like this
>>> 5.__add(8)

However Python responded
SyntaxError: invalid syntax

I tried
>>> help(5.__add__)

but got
SyntaxError: invalid syntax

However when I tried with a list
>>> help([5,6].__add__)

I got
Help on method-wrapper object:

__add__ = class method-wrapper(object)
 |  Methods defined here:
 |  __call__(...)
 |      x.__call__(...) <==> x(...)
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==>

Not that I understand much of this but at least I got some response.

Now I went to Python Library Reference and searched for "__add__" but
got zero hits.

Could someone explain the use of __add__ (and similar double underscore
attributes) and what their use is.


More information about the Python-list mailing list