方法一
但无法编写foo函数体里面内容
import types def foo(x,y): #print(x,y) return 1 x = 1 y = 2 f = types.FunctionType(foo.__code__, {}, name='test_',argdefs=(x,y))
print(f.__name__) print(f(1,2))
方法2
from utils.create_function import create_function_from_parameters from inspect import Parameter, Signature def foo(arg): print(arg) return 1 x = 1 y = 2 f = create_function_from_parameters( func=foo, parameters=[Parameter('x', Parameter.POSITIONAL_OR_KEYWORD), Parameter('y', Parameter.POSITIONAL_OR_KEYWORD), ], documentation="some doc", func_name="test_", func_filename="main.py", ) print(f.__name__) print(f(x,y))
标签:__,函数,parameters,python,动态创建,print,foo,Parameter,name From: https://www.cnblogs.com/shuzf/p/16994251.html