Altering the namespace of the calling function

Andrew Wilkinson ajw140NO at SPAMyork.ac.uk
Wed Oct 29 05:33:02 EST 2003


Hi,

First off I know that in almost all cases this would be a terrible thing to
do, but this is an unusual case where this makes sense.

Basically I have a procedure where you pass a string containting a template
for a tuple, the function then finds a tuple that matches this template and
then returns it. The template may contain variable names, which match
against anything in that position in the tuple - this is all working fine,
except that I would like a variable to be created in the caller's scope by
the matching function.

I can retrieve the scope of the calling function using
sys._getframe(1).f_locals however whenever I try to modify it the changes
are made but the variable doesn't match to the new variable in the scope. I
tried creating a variable of the same name before calling the matching
function, but when the matching function returns the changes to the scope
for that variable are discarded.

Can anyone suggest how to make this work?
Andrew Wilkinson





More information about the Python-list mailing list