python中合并函数_如何在python中合并函数

这就是Python作为一种动态语言的真正好处所在——很容易实现这样的功能。在class DynamicMethods(object):

def __init__(self):

pass

class HiddenImplementation(object):

''' we create a separate implementation class that:

1. stores the 'type' of the call (or whatever else you need to

store on a per-call basis)

2. define a __call__() method that lets us perform whatever our

dynamic method call needs to do. In this example, we just print

the kwargs we're passed along with the call 'type'.

'''

def __init__(self, typeName):

self.typeName = typeName

def __call__(self, **kwargs):

args = kwargs

args['type'] = self.typeName

for k, v in args.items():

print "{0}: {1}".format(k, v)

def __getattr__(self, name):

''' any time code looks up an attribute that the class doesn't have

explicitly, this will be called. If the attribute being looked up

starts with 'get', we create a HiddenImplementation object and

return that.

'''

if name.startswith('get'):

return self.HiddenImplementation(name)

if __name__ == "__main__":

d = DynamicMethods()

d.getId(a=1, b=2, c=3)

…指纹

^{pr2}$

你可能感兴趣的:(python中合并函数)