我大部分工作正常。我想要一個類裝飾器(Decorator
類),它接受可用於在對象上包裝方法的參數(greeting
和farewell
)(Person
的實例)。帶參數的Python類裝飾器,但沒有運行裝飾函數
一切工作正常,除...原始命令功能Person
類永遠不會運行!如果我使用類似
output = getattr(instance, func.func_name)(command, *args, **kwargs)
手動調用函數我得到無限遞歸。
我該怎麼做?完整的代碼如下:
import functools
class Decorator(object):
def __init__(self, greeting, farewell, *args, **kwargs):
print "initing"
self.greeting = greeting
self.farewell = farewell
def __call__(self, func, *args, **kwargs):
print "CALLING"
@functools.wraps(func)
def wrapper(instance, command, *args, **kwargs):
return "%s, %s! %s!\n%s, %s!" % (
self.greeting,
instance.name,
command,
self.farewell,
instance.name
)
return wrapper
class Person(object):
def __init__(self, name):
self.name = name
@Decorator("Hello", "Goodbye")
def command(self, data):
return data + "LIKE A BOSS"
s = Person("Bob")
print s.command("eat food")
實際輸出:
initing
CALLING
Hello, Bob! eat food!
Goodbye, Bob!
預期輸出:
initing
CALLING
Hello, Bob! eat food LIKE A BOSS!
Goodbye, Bob!
根據您的使用示例,'__call__'只需要一個參數:'func'。 –