On Tue, Jun 14, 2011 at 5:27 PM, Jan Kaliszewski <zuo@chopin.edu.pl> wrote:
Alex Light dixit (2011-06-14, 13:44):

> >>>            frmglobals = func.__globals__.copy()
> >>>            func.__globals__.update(localArgs)
> >>>            ret= func(*args, **kwargs)
> >>>            func.__globals__.clear()
> >>>            func.__globals__.update(frmglobals)

Changing global state on each call seems to be both
concurrency-and-recurrency-unsafe and inefficient.

well some of your safety concerns can be allayed, i hope, by replacing this snipet:

>>>            frmglobals = func.__globals__.copy()
>>>            func.__globals__.update(localArgs)
>>>            ret= func(*args, **kwargs)
>>>            func.__globals__.clear()
>>>            func.__globals__.update(frmglobals)

with this one:

>>>            with _modifyGlobals(func.__globals__, localArgs):
>>>                ret = func(*args, **kwargs)

with  _modifyGlobals defined as:

>>>from contextlib import contextmanager 
>>>
>>>@contextmanager
>>>def _modifyGlobals(glbls, additions):
>>>    frmglbls = glbls.copy()
>>>    try:
>>>        glbls.update(additions)
>>>        yield
>>>    finally:
>>>        glbls.clear()
>>>        glbls.update(frmglbls)

as for performance you are correct that it is more efficient to just use global variables.
the dictionary updates add about 1 x 10**-4 (or, if the check for collisions with KWargs is removed, 5 x 10**-5) 
seconds  to the run time of a function, at least on this computer.
so not terribly significant just be sure to use sparingly

also with the link you mentioned i could not seem to get it to work. Whenever i tried to use any built-in functions
it would start throwing NameErrors. also that is only useful if you want to inject all global variables into the function.

--Alex