Is access to locals() of "parent" namespace possible?

Carsten Haese carsten at uniqsys.com
Fri Jul 27 23:01:29 CEST 2007


On Fri, 2007-07-27 at 20:46 +0000, André wrote:
> I want to give a user the possibility of "restarting" an interactive
> session, by removing all the objects defined by her since the
> beginning.  The way I make this possible is by having a "function"
> that can be called during the interactive session using locals() as an
> argument, as follows:
> 
> restart(locals())
> 
> It works.  However, I would like to make this "friendlier", i.e.
> requiring the user to simply type
> 
> restart()
> 
> and have the code extract out the value of locals() of the "parent"
> namespace.  I thought it might be possible using sys._getframe, but I
> have not been able to figure out why.

inspect.currentframe(1).f_locals

The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list