Does python always need to compile ENTIRE program before itcanstart to run it???

Peter Hansen peter at engcorp.com
Mon Nov 3 16:08:08 EST 2003


"Michael T. Babcock" wrote:
> 
> >"Michael T. Babcock" wrote:
> >
> >
> >>> ... that'll still run 'fine' because you don't use "a"
> >>>
> >>> That said, the "compilation" time of a Python program is almost
> >>> nonexistant in most cases.  Most of the work is runtime; load a class,
> >>> wait for the class to compile, and so on..
> >>
> >>
> >
> >That's not *exactly* true, I'm afraid.  The above example would
> >actually raise a NameError, since the name "help" is not defined
> >at the time of execution, if you tried constructing an a().
> >
> 
> You seem to have completely missed my saying 'because you don't use "a"'
> in that message.  In fact, I actually ran that program to check before
> making the post.  For what its worth, if you had "help = " instead of
> just "help", it would raise a SyntaxError on execution.  However, it
> doesn't know if "help" is a valid statement or not yet because it hasn't
> evaluated 'a' and therefore leaves you alone about it.

No, Michael, you're misinterpreting what happens.  Try putting that 
"help =" in and see the difference.  You actually get the SyntaxError
*during import*, and more specifically during compilation, not execution.

The NameError from the lone "help" line would on the other hand 
come during execution, as you say *if* it were actually executed.

> >Therefore I believe your first statement is actually not true, and
> >in fact Python code that "wouldn't compile" actually cannot be run,
> >because it has to be compiled prior to running.
> 
> It does not throw a SyntaxError, and it does in fact run.  Have a go at it.

I did, and of course it works, but that's *because it would not raise
a SyntaxError* which is a compilation error, but in fact raises a NameError
which is a runtime exception, and therefore quite different.

-Peter




More information about the Python-list mailing list