473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

double underscore attributes?

Entering
dir(5)
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') <==> 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

Dec 10 '05 #1
6 2564
bo*******@yahoo.com wrote:
Now I went to Python Library Reference and searched for "__add__" but
got zero hits.


http://python.org/doc/2.4.2/ref/specialnames.html
Dec 10 '05 #2
Hi Bob,
see this my example:
class A: .... def __add__(a,b):
.... print a,b
.... a = A()
b = A()
a + b <__main__.A instance at 0x80c5a74> <__main__.A instance at 0x80c6234>


Dec 10 '05 #3
bo*******@yahoo.com wrote:
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


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

13

HTH,

STeVe
Dec 10 '05 #4
bo*******@yahoo.com wrote:
....
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
help(5.__add__)


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

Dec 10 '05 #5
bo*******@yahoo.com wrote:
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.
Dec 10 '05 #6
bo*******@yahoo.com wrote:
Entering
dir(5)

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)

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.
5 . __add__(9) 14


[...]
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mikael Petterson | last post by:
Hi, I got some help to go from: bbBusState to BB_BUS_STATE. However I found out :-( that the xml contained attributes like: TxDeviceGroup and that becomes:
2
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape...
8
by: Ahjay Muscha | last post by:
What's the best way to convert from a double to a string? for example, at the moment I have to do this: string s = new Double(a + b / c).ToString(); Is there a better way for me to do this...
4
by: simon | last post by:
which data control allows me to use a cell doubleClick event? I have time cells (something like Outlook calendar) and when user double click on one cell I would like to redirect to other window...
1
by: rmgalante | last post by:
I have written an ASP.Net application that uses the standard client-side and server-side validation for various fields on the form. Some of the customers that use the form report symptoms that...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
4
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. I notice for form elements that contain spaces, PHP substitutes an underscore for the element name when the form is submitted. For example, if I have this page...
20
by: benhoyt | last post by:
Hi guys, I've been using Python for some time now, and am very impressed with its lack of red tape and its clean syntax -- both probably due to the BDFL's ability to know when to say "no". ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.