Please explain Python "__whatever__" construct.

B

bsagert

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?
 
J

Jason Scheirer

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-_
 
S

s0suk3

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

Matimus

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__":
'sys'

Also, these names are frequently used when creating a class where you
want special behavior.
.... def __init__(self, a): # The constructor
.... self.a = a
....
.... def __add__(self, x):
.... print "I'm adding"
.... return self.a + x
....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:
File "<stdin>", line 1
x "world"
^
SyntaxError: invalid syntax

Where:
'hello world'

Matt
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top