https://stackoverflow.com/questions/13184281/python-dynamic-function-creation-with-custom-names

 

def bindFunction1(name):
    def func1(*args):
        for arg in args:
            print arg
        return 42 # ...
    func1.__name__ = name
    return func1

def bindFunction2(name):
    def func2(*args):
        for arg in args:
            print arg
        return 2142 # ...
    func2.__name__ = name
    return func2

 

 

反射的方法

 

class FunctionTemplate(object):
    def __init__(self):
        self.__dict__={}
    def __getattr__(self,attr):
        def p(*args):
            print ("Function name: %s, No. of args: %d"%(attr,len(args)))
            return
        self.__dict__[attr]=p
        return p

fnames=["func1","func2","func3"]
x=FunctionTemplate()
a=[getattr(x,i) for i in fnames]

 

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2021-06-07
  • 2021-09-20
  • 2021-08-08
猜你喜欢
  • 2022-12-23
  • 2021-09-13
  • 2021-07-08
  • 2021-11-16
  • 2022-12-23
  • 2021-12-10
  • 2021-10-25
相关资源
相似解决方案