簡體   English   中英

班級裝飾

[英]Class level decorator

我正在嘗試使用以下代碼進行一種類型處理函數注冊:

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)

但我更喜歡功能注冊而不是基於名稱的查找。

我同意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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM