[Python-Dev] Example for "property" violates "Python is not a one pass compiler"

Nick Coghlan ncoghlan at gmail.com
Tue Sep 6 16:24:21 CEST 2005


Greg Ewing wrote:
> Phillip J. Eby wrote:
> 
>>I'm not sure where you got the "Python is not a one pass compiler" idea; I 
>>don't recall having seen this meme anywhere before, and I don't see how 
>>it's meaningful anyway.
> 
> 
> Indeed, Python's bytecode compiler essentially *is*
> a one-pass compiler (or at least it used to be -- not
> sure what's been done to it recently).

It builds the symbol table before actually trying to compile anything. This is 
what allows it to figure out which load commands to use for which symbols.

I'm not up to speed on my compiler theory though, so I'm not sure if building 
the symbol table first is enough to count as two-pass compilation.

> But the behaviour seen here is more about what happens
> at run time than compile time. What you're trying to
> do is essentially the same as
> 
>     print x
>     x = 42
> 
> which fails at run time because x hasn't been bound
> when the print statement is executed.

I've found this to be one of the subtler habits to try and break when coming 
from a static language - in something like C++, functions and classes are 
interpreted at compile time, and the result made part of the executable. In 
Python, the only thing created at compile time is the bytecode required to 
create the class or function - the definition isn't actually processed until 
runtime. It's easy to forget that difference (mainly because it doesn't matter 
very often).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list