我正在尝试使用以下代码进行一种类型处理函数注册:
types = {}
def type_handler(name):
def wrapper(f):
types[name] = f
return f
return wrapper
@type_handler('a')
def handle_a(a):
...
@type_handler('b'):
def handle_b(b):
...
def handle(x):
types[x.name](x)
这很好用,但现在我想让它在课堂上工作 我试过这个:
class MyClass(object):
types = {}
def type_handler(name):
def wrapper(f):
types[name] = f ## global name 'types' is undefined
return f
return wrapper
@type_handler('a')
def handle_a(self, a):
...
@type_handler('b'):
def handle_b(self, b):
...
def handle(self, x):
self.types[x.name](self, x)
但它说global name 'types' is undefined
我尝试将其更改为
def type_handler(name):
def wrapper(f):
MyClass.types[name] = f ## global name 'MyClass' is undefined
return f
return wrapper
但现在它说global name 'MyClass' is undefined
我能做些什么来完成这项工作?
我知道我可以这样做:
def handle(self, x):
self.__getattribute__('handle_%s' % x.name)(self, x)
但我更喜欢功能注册而不是基于名称的查找。
答案 0 :(得分:3)
我同意Sven Marnach的观点。你应该使用“少魔法”。但是,这是一个快速修复:
#decorator is declared outside of class and type dict is passed in as an argument
def type_handler(name, type_dict):
def wrapper(f):
type_dict[name] = f
return f
return wrapper
class MyClass(object):
types = {}
@type_handler('a', types)
def foo_a(self, a):
pass
@type_handler('b', types)
def foo_b(self, b):
pass
版本2
这个使用一个类作为装饰器:
class TypeHandler(object):
def __init__(self, type_dict):
self.types = type_dict
def __call__(self, name):
def wrapper(f):
self.types[name] = f
return f
return wrapper
class MyClass(object):
types = {}
thandle = TypeHandler(types)
@thandle('a')
def foo_a(self, a):
pass