[SciPy-dev] How to get extensions to affect local state

Pearu Peterson pearu at cens.ioc.ee
Mon Feb 11 12:53:07 CST 2002


On Fri, 8 Feb 2002, Pat Miller wrote:

<snip>

> It is a bit tricky as you cannot just use python tricks to update
> the local dictionary for affect:
> 
> That is if inside weave.inline, it makes a call to locals() (or
> rather gets it from the caller's frame) you can't make changes.

See http://www.python.org/doc/current/lib/built-in-funcs.html:
locals() 
     Return a dictionary representing the current local symbol
     table. Warning: The contents of this dictionary should not be
     modified; changes may not affect the values of local variables used
     by the interpreter. 

And I think you should not modify locals() dictionary from Python
C/API either. 

Actually, there are no tricks needed to get what you want in Python. 
For example,

import sys
def fun(varname):
    frame = sys._getframe(1)
    exec '%s = 7' % (varname) in frame.f_locals
a = 5
print a
fun('a')
print a

will output

5
7

Regards,
        Pearu






More information about the Scipy-dev mailing list