472,345 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Please explain Python "__whatever__" construct.

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
3 1409
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to...
0
by: Henrik Nordgren | last post by:
I have written some previous remotings using both singlecall and singleton CAOs. And for every project I always defined an interface between the...
12
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? ////////////////////////////////////////////////...
1
by: Simon Windsor | last post by:
Hi I have just recevived this error could not write to hash-join temporary file: No space left on device Can someone please explain how I...
1
by: John | last post by:
I am having trouble understanding how to use different properties. For example, I have a maskedtext box on a form. Lets call it msk1. I want to...
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main()...
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'};...
1
by: AnagJohari | last post by:
please tell me how ro define a header file for a perticular function so i can use in another file please explain by giving example of any simple...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.