exec and CodeObjects

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Jul 21 08:56:53 CEST 2007


En Fri, 20 Jul 2007 12:48:05 -0300, Prepscius, Colin (IT)  
<Colin.Prepscius at morganstanley.com> escribió:

> Does anybody know how to pass parameters to 'exec
> somefunction.func_code'?
> def f1():
>     print 'this is f1'
> def f2(p):
>     print 'this is f2, p =', str(p)
> exec f1.func_code
> THIS RESULTS IN:  "this is nf1"   WHICH IS NICE
> exec f2.func_code
> THIS RESULTS IN:  TypeError: f2() takes exactly 1 argument (0 given)
> WHICH IS EXPECTED

Instead of using exec, rebuild a new function from the unmarshalled code:

import new
f3 = new.function(f2.func_code, globals())
f3(parameter)

-- 
Gabriel Genellina




More information about the Python-list mailing list