动态生成指定函数名的函数

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]

 

转载于:https://www.cnblogs.com/clemente/p/10405066.html

你可能感兴趣的:(javascript)