statically nested scopes

Paul Prescod paulp at
Mon Nov 6 01:41:36 CET 2000

Robin Becker wrote:
> Isn't there a way to affect the locals dynamically such as eval 'G=i*i'
> out.

I'm not following you about how statically nested scopes makes this
worse. Python's handling of these corner cases is pretty wonky today. I
don't see how statically nested scopes makes this any worse:

>>> G=0
>>> def F1():
...     print G
...     exec "G=2"
...     print G
>>> def F2():
...     print G
...     G=2
...     print G
>>> F1()
>>> F2()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in F2
UnboundLocalError: Local variable 'G' referenced before assignment

If anything, static scopes could make this more regular by binding G at
compile time *always* instead of just *sometimes*. i.e. we could change
the behaviour of the "exec" version to be the same as the behavior of
the straight version.

 Paul Prescod

More information about the Python-list mailing list