By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,486 Members | 1,197 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,486 IT Pros & Developers. It's quick & easy.

Please explain Python "__whatever__" construct.

P: n/a
After a couple of weeks studying Python, I already have a few useful
scripts, including one that downloads 1500 Yahoo stock quotes in 6
seconds. However, many things are puzzling to me. I keep on seeing
things like "__main__" in scripts. A more obscure example would be
"__add__" used in string concatenation. For example, I can use "Hello
"+"world (or just "Hello" "world") to join those two words. But I can
also use "Hello ".__add__("world"). When and why would I ever use
"__main__" or the many other "__whatever__" constructs?
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
On Jun 16, 2:56*pm, bsag...@gmail.com wrote:
After a couple of weeks studying Python, I already have a few useful
scripts, including one that downloads 1500 Yahoo stock quotes in 6
seconds. However, many things are puzzling to me. I keep on seeing
things like "__main__" in scripts. *A more obscure example would be
"__add__" used in string concatenation. For example, I can use "Hello
"+"world (or just "Hello" "world") to join those two words. But I can
also use "Hello ".__add__("world"). When and why would I ever use
"__main__" or the many other "__whatever__" constructs?
http://docs.python.org/lib/genindex.html#letter-_
Jun 27 '08 #2

P: n/a
On Jun 16, 4:56 pm, bsag...@gmail.com wrote:
After a couple of weeks studying Python, I already have a few useful
scripts, including one that downloads 1500 Yahoo stock quotes in 6
seconds. However, many things are puzzling to me. I keep on seeing
things like "__main__" in scripts. A more obscure example would be
"__add__" used in string concatenation. For example, I can use "Hello
"+"world (or just "Hello" "world") to join those two words. But I can
also use "Hello ".__add__("world"). When and why would I ever use
"__main__" or the many other "__whatever__" constructs?
Generally, names with two leading and trailing underscores signal
something "internal". Though the string "__main__" is rather something
else: the variable __name__ is set to the string "__main__" when a
script is run as a script (i.e., is not imported). The convention is
also common in built-in object methods, such as the one you mentioned:
the built-in type str's __add__() method. Personally, I usually try to
avoid using such methods directly, because, as I said, they're rather
for internal use or for special functionality. For example, when the
expression '"hello" + "world"' is evaluated, it's likely that Python
is calling one of the string's __add__() method internally to perform
the "addition." So I'd recommend that you don't use those methods
unless you absolutely need direct access to their functionality.
Jun 27 '08 #3

P: n/a
When and why would I ever use
"__main__" or the many other "__whatever__" constructs?
You don't generally use those names directly, they are 'magic'. The
__add__ example is a good one. When you do `"hello " + "world"` behind
the scenes python is actually calling "hello ".__add__("world").

There are a couple of places though that you do use them. "__main__"
is a good example. That is the name of the `main` module. The module
attribute `__name__` is the name of that module. If the code is being
executed as a script the value of `__name__` is set to "__main__".
Hence, if you create a module and you want to execute some code only
if that module is run as a script you can use this construct:

if __name__ == "__main__":
# do stuff

Here is an example of a the `__name__` attribute when it isn't
"__main__":
>>import sys
sys.__name__
'sys'

Also, these names are frequently used when creating a class where you
want special behavior.
>>class myint(object):
.... def __init__(self, a): # The constructor
.... self.a = a
....
.... def __add__(self, x):
.... print "I'm adding"
.... return self.a + x
....
>>x = myint(10)
x + 12
I'm adding
22

As an added note, `"hello " "world"` is not concatenating two strings,
The parser just sees it as one string. Otherwise, this would also
work:
>>x = "hello "
x "world"
File "<stdin>", line 1
x "world"
^
SyntaxError: invalid syntax

Where:
>>x = "hello "
x + "world"
'hello world'

Matt
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.