Robin Becker wrote:
.... If I have messed that up then there should be some easy fix, otherwise if pycodegen is somehow not getting the semantics of the the variables i,j correct is there some way I can fix that....

def func(D):
    for k in D:
        exec '%s=D[%r]' % (k,k)
    print i, j, k
    print locals()
    print i, j, k

if __name__=='__main__':
    func(dict(i=1,j=33))
#### end p.py

the compiler package ends up treating i & j as global, whereas the modern analysis doesn't (and doesn't say they're definitely local either).

If they are not definitely local, they are non-local.  Locals are
determined at function definition time, not function execution time.
So, your expectations about what the exec statement can do above are
mistaken.  You may try to work your way around it, but, IMHO, you
will not succeed.  If the code above were to work as you wish, every
access to every non-local in code that contains an "exec" would have
to check a "new locals" dictionary just in case the exec added a local.
Think about what this code would have to do:
> i = j = 42
> def func(D):
>     print i, j, k
>     for k in D:
>         exec '%s=D[%r]' % (k,k)
>     print i, j, k

--Scott David Daniels
scott.dani...@acm.org

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to