How does python build its AST

Carl Banks pavlovevidence at gmail.com
Sat Dec 8 04:32:28 EST 2007


On Dec 7, 9:23 am, MonkeeSage <MonkeeS... at gmail.com> 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?)

Python creates certain objects at compile time but doesn't bind them
to names in the modulespace until run time.

Python could--and many other languages do--automatically bind these
objects to names upon import.  Python doesn't do it because it sees a
module as "code to be executed" rather than a "list of global object
definitions".

Something like this would be awkward if Python bound the names at
import time:

if X:
    def a(): do_this()
else:
    def a(): do_that()

Which one gets bound to a?

To do something similar in C would require preprocessor macros (ick).


Carl Banks



More information about the Python-list mailing list