Functions and code objects

Duncan Booth duncan.booth at invalid.invalid
Thu Jul 27 10:58:30 EDT 2006


Fuzzyman wrote:

>> I'd like to construct the code object so that it takes the parameters
>> from the enclosing scope (the context I pass into exec I guess),
>> without clobbering any local variables that may be defined in the code
>> object.
>>
>> Anyone got any clues ?

Does this do what you wanted? Instead of messing about with the code object 
just work out which values from the namespace the function actually 
expects.

>>> def callfromnamespace(fn, namespace):
    names = fn.func_code.co_varnames[:fn.func_code.co_argcount]
    fn(**dict((name, namespace[name])
        for name in names if name in namespace))

    
>>> def f(x, y=99):
    z = 2
    print x, y, z

    
>>> x = 42
>>> callfromnamespace(f, globals())
42 99 2
>>> y = 37
>>> callfromnamespace(f, globals())
42 37 2
>>> def testme():
    x = 3
    callfromnamespace(f, vars())
    y = 9
    callfromnamespace(f, vars())

    
>>> testme()
3 99 2
3 9 2



More information about the Python-list mailing list