[Python-Dev] 2.5 and beyond

Phillip J. Eby pje at telecommunity.com
Sat Jul 1 01:09:41 CEST 2006


At 07:04 PM 6/30/2006 -0400, Andrew Koenig wrote:
>However, if I write
>
>         def g():
>                 return x
>         x = 42
>         g()
>
>the result is 42.  With lexical scoping, I believe it should be undefined.
>
>The reason is that when the compiler encounters the definition of g,
>variable x is not yet bound, and there is nothing in the body of g that
>would bind it.  Therefore, g requires a binding to exist at the time it is
>compiled; because no such binding exists, this example would be an error (at
>compile time) under lexical scoping.

That sounds like a bug, not a feature.  It's frequently useful to have 
forward references in function bodies to names that are not yet globally 
bound, e.g. for classes, or mutually-recursive functions.



More information about the Python-Dev mailing list