statically nested scopes

Robin Becker robin at jessikat.fsnet.co.uk
Mon Nov 6 00:59:24 CET 2000


In article <mailman.973448716.22393.python-list at python.org>, Paul
Prescod <paulp at ActiveState.com> writes
>On Sat, 4 Nov 2000, Robin Becker wrote:
>
>> How can this work when different paths define different 
>> variables.
>
>You're probing a dark corner of Python, but I don't think that 
>statically nested scopes makes it any worse than it is.
>
>> ie
>> 
>> G=0
>> def A():
>>   for i in range(3):
>>     if i>=2:
>
>>       G=i*i
>>     def B():
>>       return G+2
>>     print B()
>
>I claim the answer would be the same as if you remove the
>nested function altogether:
>
>G=0
>def A():
>    for i in range(3):
>        if i>=2:
>            G=i*i
>        print G
>A()
>
>It is all figured out statically, not dynamically so G is either 
>always a local or always a global -- in this case always a local
>because it is assigned to.
>
> Paul Prescod
>
>
>
Isn't there a way to affect the locals dynamically such as eval 'G=i*i'
if I do
>>> def F():
...     print locals()
...     exec 'G=2'
...     print locals()
... 
>>> F()
{}
{'G': 2}

so that I could alter my original A to sometimes generate a local and
sometimes not! It would need a smart compiler to figure all such cases
out.
-- 
Robin Becker



More information about the Python-list mailing list