double underscore attributes?

B

bobueland

Entering
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
However Python responded
SyntaxError: invalid syntax

I tried
but got
SyntaxError: invalid syntax

However when I tried with a list

I got
Help on method-wrapper object:

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

Bob
 
S

Steven Bethard

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


However Python responded
SyntaxError: invalid syntax

I tried


but got
SyntaxError: invalid syntax

Note that these SyntaxErrors are due to how Python parses floats:
Traceback ( File "<interactive input>", line 1
5.__add__(8)
^
SyntaxError: invalid syntax13

HTH,

STeVe
 
D

Dan Bishop

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 ....
I tried

but got
SyntaxError: invalid syntax

That's because the parser thinks "5." is a float, rather than the
integer 5 with a dot after it. If you want to refer to an attribute of
an integer literal, you can use "(5).__add__" or "5 .__add__" (with a
space).
 
X

Xavier Morel

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

Bob
Methods with double leading and trailing underscores are basically
"magic methods" with specific meanings for the Python interpreter.

You're not supposed to use them directly (in most cases) as they are
wrapped by syntactic sugar or part of protocol's implementation.

__add__, for example, is the definition of the "+" operator, using
"(5).__add__(8)" is exactly the same as using "5+8".

To get a list of most of these magic methods, check the Python
documentation on the "operator" module.

These magic methods allow you to emulate numbers, sequences, iterators,
or even callables (allowing you to use an object as a function). Be
really careful with them though, one of the things that plagued (and
still plague) C++ is the abuse of operators overloading and modification
of their meaning.
 
S

Steve Holden

Entering


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
Bzzt.

Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
[...]
regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top