我正在尝试使用以下代码进行一种类型处理函数注册:
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)
但我更喜欢函数注册而不是基于名称的查找。