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

Peter Hansen peter at engcorp.com
Mon Nov 3 21:21:04 CET 2003


"Michael T. Babcock" wrote:
> 
> 
> > make ; myprogram
> >
> >
> >I am trying to think of an advantage Python has over this hack....
> >
> >Is it true that unlike C/C++ that Python programs can start executing
> >before compilation is COMPLETELY done???  I think so but I'm not sure.
> >
> 
> Yes.  You can have Python code that wouldn't "compile" but still runs.
> As long as the paths the interpreter takes have no errors, you're fine
> (minor exception of syntax errors).
> 
> class a:
>     def __init__(self):
>        help # syntax error if we initialize an instance of a()
> 
> print "hello world"
> 
> ... 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().

SyntaxErrors for the most part come during compilation only (I'm
not sure if there are any exceptions to that, but I'm not aware
of any), and compilation is done on a module *prior* to execution,
unlike some other languages where the source itself is interpreted
and there is not really a compilation phase.

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.

-Peter




More information about the Python-list mailing list