参考文章https://learnku.com/articles/25747
types.functionType的使用
首先编译字符串
funcStr="def func(t): print(t)"
funcCode=complie(funcStr,"<string>","exec")
然后通过types.functionType创建函数类型
myfunc=FunctionType(funcCode.co_consts[0], globals(), "foo")
funcCode.co_consts 包含了code对象,方法名等等
这里一定要funcCode.co_consts[0],表示将code对象作为参数传入,网上看的好多代码都是错的
随后可以将参数改为def func(self),这样可以作为动态类的方法