Scope in 2.2.1

Terry Reedy tjreedy at udel.edu
Sat May 11 08:28:41 EDT 2002


"David LeBlanc" <whisper at oz.net> wrote in message
news:mailman.1021115239.6564.python-list at python.org...

> I have grown accustomed by long use to the way that C, C++ and
Pascal (and
> asm for that matter!) become aware of declarations. To use an
analogy,
> there's a line that moves down the page and things below the line
are
> unknown to the compiler since they're not seen yet unless they're
forward
> declared. Code is parsed based on what's known above the line.

Python is Python -- and not anything else.

> Python seems
> to have an implicit per block pre-pass that gets all the bindings
before
> statements are parsed. Is this the correct idea?

Yes.  Python functions have, in a sense, *two* runtimes.  First is
definition time during which three actions are perfomed:
1. process parameter list, which includes evaluating default value
expressions and putting param names on internal local variable list;
2. scan body text for name bindings and add bound names to local
variable list;  as an optimazation, this is done while the body is
compiled to byte code, but it could be done even if there were no
byte-code compilation;
3. assemble function object and bind it to the definition name.

The second runtime is call time during which the (compiled) body is
executed, using the information, including local var list, gathered
during defination-construction time.

Generators add a third 'runtime' -- iterator-construction -- inbetween
definition and evaluation, but thats another story.

Terry J. Reedy






More information about the Python-list mailing list