When I get hold of a code object (via internals like .func_code), is there any way of calling this piece of code? Simply calling it does not work:
def f(): pass
f.func_code()
results in
Traceback (most recent call last):
File "", line 1, in
TypeError: 'code' object is not callable
This can come in handy when you like to unit-test a nested function:
def f():
def g():
return 3
f.x = g
return g() + 1
f.func_code.co_consts[1]
results in
", line 2>
Of course, this piece of code still needs a context etc. but that's not my question here.
解决方案
One can eval() or exec them.
If they have free variables (e. g. for the code of nested functions whose outer function has local variables defined before the nested function), this is not directly possible (eval or exec raise a TypeError in this case).
Also, passing arguments to the code is not directly possible.
But one can create a wrapping function for the given code on-the-fly. This function can normally be called (using f(…)), passing arguments in the usual way. This is done using types.FunctionType. To achieve references to free variables, one must use a trick in order to get the correct data type as Python expects it. See the code below for an example:
def f(v1=1):
v2 = 2
def g(v3=4):
return v1 + v2 + v3 + 8
return g() + 16
def freeVar(val):
def nested():
return val
return nested.__closure__[0]
def nested(outer, innerName, **freeVars):
if isinstance(outer, (types.FunctionType, types.MethodType)):
outer = outer.func_code
for const in outer.co_consts:
if isinstance(const, types.CodeType) and const.co_name == innerName:
return types.FunctionType(const, globals(), None, None, tuple(
freeVar(freeVars[name]) for name in const.co_freevars))
nestedG = nested(f, 'g', v1=1, v2=2)
print nestedG(4) # will print 15