Altering the namespace of the calling function

Emile van Sebille emile at fenx.com
Wed Oct 29 09:55:36 EST 2003


Andrew Wilkinson asks:
> except that I would like a variable to be created in the caller's scope by
> the matching function.

Pass in a mutable or use return.  The obvious intent is to use this in the
calling routine, and you're asking how to change the called routine, so
you've got access to both.

>
> 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

No surprise here.  From the manual:
"The contents of this dictionary should not be modified; changes may not
affect the values of local variables used by the interpreter. "


> 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?

If both routines are within the same global module scope, you could use
global.  If not, you could hang shared variables on a mutually imported
module.

HTH,

Emile van Sebille
emile at fenx.com







More information about the Python-list mailing list