How does python build its AST

Michael Spencer mahs at telcopartners.com
Fri Dec 7 15:45:53 EST 2007


MonkeeSage wrote:
> A quick question about how python parses a file into compiled
> bytecode. Does it parse the whole file into AST first and then compile
> the AST, or does it build and compile the AST on the fly as it reads
> expressions? (If the former case, why can't functions be called before
> their definitions?)
> 
> Thanks,
> Jordan
Python (2.5+) does parse the whole file top-to-bottom into an AST, and then 
compile the entire AST, resulting in a module code object that may contain other 
compiled code objects for the function bodies.

However, this isn't relevant to your parenthesized question, since compiling 
functions does not make them callable.  A function becomes available only once 
its def statement has been executed.  Since execution (generally) proceeds top 
to bottom, this is why functions can't be called before their definitions.

HTH
Michael





More information about the Python-list mailing list