if __name__ == 'main': & passing an arg to a class object
Duncan Booth
duncan.booth at invalid.invalid
Mon Apr 30 07:21:18 EDT 2007
Bart Willems <b.r.willems at gmail.com> wrote:
> gtb wrote:
>> appear at the end of many examples I see. Is this to cause a .class
>> file to be generated?
> This might be obvious, but no one else mentioned it: the Python
> interpreter cannot execute code that it hasn't compiled yet, which is
> why the "if __name__ ..." code is always at the end of the module - to
> guarantee that the entire file is scanned first.
This is somewhat misleading: the entire file is always compiled before any
of it is executed (to see this try putting a syntax error on the last line:
the syntax error will prevent any lines in the file being executed).
A more accurate statement would be to say that executing code cannot access
any names which have not yet been defined. The code for functions and
classes is compiled first with everything else, but the function or class
objects are not created, and the relevant names are not bound until the
appropriate 'def' or 'class' statement is executed.
More information about the Python-list
mailing list