Bart Willems <[EMAIL PROTECTED]> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to