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

William Trenker wtrenker at shaw.ca
Sun Sep 14 12:33:49 EDT 2003


On Sun, 14 Sep 2003 21:45:16 +0000 (GMT)
Don Bruder <dakidd at sonic.net> wrote regarding Huh?!?!? What is it that I'm seeing here?:

> 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





More information about the Python-list mailing list