__main__ : What is this?

R

Robert Dailey

Hi,

I've read various portions of the Python 2.5 documentation in an
attempt to figure out exactly what the following condition represents:

if __name__ == "__main__":
main()

However, I was not able to determine what it is actually checking for.
Could someone point me in the way of a tutorial or explain this for
me? Thanks.
 
M

Matimus

I've read various portions of the Python 2.5 documentation in an
attempt to figure out exactly what the following condition represents:

if __name__ == "__main__":
main()

However, I was not able to determine what it is actually checking for.
Could someone point me in the way of a tutorial or explain this for
me? Thanks.

__name__ is an attribute on a module that shows the standard name for
that module:
'sys'

The name stays constant even if you do something funny during import:
'os'

When using the interpreter or running a python script code will be
executed in a standard module called "__main__".
'__main__'

In fact, you can even import __main__ if you want:
100

The common pattern:

if __name__ == "__main__":
# do stuff

IMHO better written:

if "__main__" == __name__:
# do stuff

Allows a module to selectively run code only if it is being run as a
program. That code will not run if it is imported as a module, because
in that condition __name__ will return the name of the file (sans .py)
that the code is in. I've never tried naming a file __main__.py and
importing it, my guess is that you shouldn't do that :).

Matt
 
G

Gabriel Genellina

The common pattern:

if __name__ == "__main__":
# do stuff

IMHO better written:

if "__main__" == __name__:
# do stuff

I'm intrigued why do you feel the second alternative is better.
Which is your native language? In English (and Spanish, and many others
but still not in the majority) the usual ordering is "subject-verb-object"
or SVO, which matches the first alternative: "If the name is __main__, do
this..."
As all the languages I know (not so many!) are SVO, I can't think of any
equivalent of the second form [that I could say it's better than the first]
 
J

Jeff

A common pattern is to put test code in the block there, too, for
modules.

Re comparison ordering, perhaps it's as in PHP, where string literals
should always go before a variable in a comparison in case evaluating
the variable causes an error :)

Mas, ese orden nunca uso yo ;).

The common pattern:
if __name__ == "__main__":
# do stuff
IMHO better written:
if "__main__" == __name__:
# do stuff

I'm intrigued why do you feel the second alternative is better.
Which is your native language? In English (and Spanish, and many others
but still not in the majority) the usual ordering is "subject-verb-object"
or SVO, which matches the first alternative: "If the name is __main__, do
this..."
As all the languages I know (not so many!) are SVO, I can't think of any
equivalent of the second form [that I could say it's better than the first]
 
S

Steven D'Aprano

The common pattern:

if __name__ == "__main__":
# do stuff

IMHO better written:

if "__main__" == __name__:
# do stuff

Apart from looking weird, what's the difference?
 
M

Matt McCredie

The common pattern:

if __name__ == "__main__":
# do stuff

IMHO better written:

if "__main__" == __name__:
# do stuff

I'm intrigued why do you feel the second alternative is better.
Which is your native language? In English (and Spanish, and many others
but still not in the majority) the usual ordering is "subject-verb-object"
or SVO, which matches the first alternative: "If the name is __main__, do
this..."
As all the languages I know (not so many!) are SVO, I can't think of any
equivalent of the second form [that I could say it's better than the first]

English is my native language (and only, I'm American :|). The reason
I like the second version better is simply that:

variable == literal

can easily be mis-written as

variable = literal

I suppose that isn't a huge issue in Python, since most of the time
comparisons happen within if and for statements. Even if it is within
a functions parameters it will raise a SyntaxError exception. There is
still the case where one might write something like this:

a = b == 'c'

or, as I prefer:

a = 'c' == b

It is just habit from writing so much C code that way. In C the
reasoning is that if you have mistyped it, you will catch the issue at
compile time instead of runtime (which is potentially much more
difficult to debug). I'm used to seeing that pattern. In terms of
natural language I do agree with you. It really is just my _humble_
opinion. I can't make a huge argument for it in Python. To be honest,
I didn't give it much thought before I wrote it. I am simply used to
doing it that way, and being irked whenever I see it written the other
way in C or C++ (and perhaps unjustifiably Python).

Matt
 
P

Peter Otten

Steven said:
Apart from looking weird, what's the difference?

In C this style is sometimes propagated as a means to avoid accidental
assignment:

if (main="main") {...} /* valid C, always true */
if ("main"=main) {...} /* syntax error */

In Python it would be cargo cult.

Peter
 
D

Duncan Booth

Matt McCredie said:
or, as I prefer:

a = 'c' == b

It is just habit from writing so much C code that way. In C the
reasoning is that if you have mistyped it, you will catch the issue at
compile time instead of runtime (which is potentially much more
difficult to debug). I'm used to seeing that pattern. In terms of
natural language I do agree with you. It really is just my _humble_
opinion. I can't make a huge argument for it in Python. To be honest,
I didn't give it much thought before I wrote it. I am simply used to
doing it that way, and being irked whenever I see it written the other
way in C or C++ (and perhaps unjustifiably Python).

Alternatively you could just tell your C compiler to regard dodgy looking
assignments as an error and stop worrying about it. Much less hassle all
round.

e.g. For gcc -Werror -Wparentheses, for Microsoft compiler -W4 -WX, or an
appropriate #pragma in a header file for each.
 

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,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top