Huh?!?!? What is it that I'm seeing here?

W

William Trenker

if __name__ == '__main__':
run(argv[1:])

The above code reads something like this: if the name of the current module is __main__, then invoke the run method with the parameter to run() being a list of the remaining arguments on the command line, excluding the argument that is the name of the python script to be executed.

The name of a module is set by Python to the value __main__ when the module is invoked from the command line. So if I have a command line like this:

python boink.py arg1 arg2 arg3

then the code for boink.py will be loaded and run. Since boink.py was loaded from the command line it will be given the module name __main__ and so the if statement will succeed. The run method will be invoked with argv[1:] expanded like this:

run(['arg1', 'arg2', 'arg3'])

(argv[0] is the value 'boink.py' from the above command line example.)

Hope that helps,
Bill
 
D

Don Bruder

First off, I'm a non-Python type person - I prefer a compiled language,
when the option is available, with a strong preference for C and/or C++.
(Sorry, boyz-n-girlz, I'm not interested in "You need to learn a real
language like _________" fights, or debates over the merits and/or
shortcomings of C/C++ - C/C++ is what I want the program I'm working
with to be written in, and that means that all discussion regarding
language choice is moot. End of discussion there.)

I'm in the process of trying to convert a Python script/program or
whatever the favored term is into C and/or C++, and of course, due to my
unfamiliarity with Python, I'm hitting some stuff that looks to be
idiomatic, without being able to locate a definition/description of the
idiom. Most of what I'm messing with seems pretty straightforward - it's
converting nicely into C++, and with my testing so far, appears to be
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This (or a markedly similar construct) sits all by its lonesome self at
the bottom of several of the source files, apparently not part of any
class or declaration. It seems to be *FAIRLY* common in the group of
files that make up the program, which makes me think it may be some sort
of "preprocessor directive" type thing, similar to a C construct that
would conditionally compile in a "main()" routine if a given symbol
is/isn't defined. In all cases where this construct appears, there's a
corresponding "run()" method, which, confusingly, appears to be outside
the scope of any lexical blocks that I can recognize, so I've got no
contextual clue there...

I've paged through all the online Python docs I can find, but although I
recall seeing a reference to __name__ as some sort of special
variable/identifier, that was when I first came up against the language,
so I paid no attention at that time. Now, when I actually NEED it, I
can't LOCATE it! Can anybody help me out here? Judging by how common it
is in the files in this program, and the fact that I simply cannot
re-find the discussion of the "__name__" thing, I have to assume it's
one of those "Duh! Anybody smarter than a sheep already knows that!"
constructs that anyone with some experience with the language has
used/understands without needing more explanation.

Unfortunately, I have little-to-no experience with Python, and even less
interest in learning it - chances are so far beyond "high" that I'll
never write a single line of code in it that the number might as well be
infinite, since I have zero interest in taking the time to learn a new
language for a one-off project. I simply want a program that was
originally (however good, bad, or in-between the original decision might
have been) written in Python turned into a C/C++ program that I can work
with effectively using existing skills and knowledge. Not
knowing/understanding the function of this construct appears to be
hindering that process, so maybe somebody here can help me out?

Thanks in advance...
 
T

Tobiah

performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This is used to check whether the script file is being
run directly as a python script (as opposed to being
imported as a module).

One use of this is to put unit tests in each module filed.
Then you can just do "python module.py" to run the code
under the if statement. When you import the module, the
test code will not be run.

Tobiah
 
D

Don Bruder

performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This is used to check whether the script file is being
run directly as a python script (as opposed to being
imported as a module).

One use of this is to put unit tests in each module filed.
Then you can just do "python module.py" to run the code
under the if statement. When you import the module, the
test code will not be run.

Tobiah
[/QUOTE]

BEAUTIFUL! Exactly the kind of information I was hoping for!

Looks like that means I can safely "lose" all occurrences, since once I
finish the port, it'll be a monolithic program.

Thanks Tobiah!
 
J

John Roth

I'm in the process of trying to convert a Python script/program or
whatever the favored term is into C and/or C++, and of course, due to my
unfamiliarity with Python, I'm hitting some stuff that looks to be
idiomatic, without being able to locate a definition/description of the
idiom. Most of what I'm messing with seems pretty straightforward - it's
converting nicely into C++, and with my testing so far, appears to be
performing properly (albeit somewhat inefficiently, since I haven't done
any tweaking yet) but one particular construct has me stumped. It looks
like this:

if __name__ == '__main__':
run(argv[1:])

This (or a markedly similar construct) sits all by its lonesome self at
the bottom of several of the source files, apparently not part of any
class or declaration. It seems to be *FAIRLY* common in the group of
files that make up the program, which makes me think it may be some sort
of "preprocessor directive" type thing, similar to a C construct that
would conditionally compile in a "main()" routine if a given symbol
is/isn't defined. In all cases where this construct appears, there's a
corresponding "run()" method, which, confusingly, appears to be outside
the scope of any lexical blocks that I can recognize, so I've got no
contextual clue there...

What's confusing you is that Python executes the module when it's
first run (or imported.) That means that any code outside of a def
is executed directly, including those last two lines that have
you puzzled.

"__main__" is the name of the module that is executed from the
command line, regardless of what its name was on disk. What
that means is that the if statement will succeed if the module is
run directly from the command line, but will fail if it is imported
from some other module.

HTH

John Roth
Thanks in advance...

--
Don Bruder - (e-mail address removed) <--- Preferred Email - unmunged, SpamAssassinated
Hate SPAM? See <http://www.spamassassin.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages:
<http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
 
J

John Hunter

Don> if __name__ == '__main__': run(argv[1:])

You can safely ignore these in your conversion project.

if __name__ == '__main__'

This means if the text file "somefile.py" holding the module code is
run as the main file, as in
> python somefile.py or
> ./somefile.py
if the file has an executable bit set

run(argv[1:])

This means "call the function run, passing command line arguments as
arguments".

Basically, the code at the bottom allows any python module to save the
same purpose as the C/C++ function 'main'. Once you have converted
the python module, and implemented the function "run" in your C/C++
code, you would do something like

int main( int argc , char** argv ) {
run(argv);
}

Cheers,
John Hunter
 
G

Gary Feldman

BEAUTIFUL! Exactly the kind of information I was hoping for!

Looks like that means I can safely "lose" all occurrences, since once I
finish the port, it'll be a monolithic program.

What you may want to do is to take the code executed by these idioms and
port them into your unit testing system. And I highly recommend unit
testing for any porting project.

Gary
 

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,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top