Let's go over a few pieces of advice concerning decorators that did not fit into earlier sections.
Decorators with arguments are an advanced topic. They can be implemented as types with a __call__
method, or as a triple-nested function. For example, the following two pieces of code behave the same.
def deco_using_func(key): def _deco(func): def inner(*args, **kwargs): print 'Hello from', key return func(*args, **kwargs) return inner return _deco class deco_using_cls(object): def __init__(self, key): self.key = key def __call__(self, func): def inner(*args, **kwargs): print 'Hello from', self.key return func(*args, **kwargs) return inner
Either one would be hooked up as follows. Note that the decorator is called with arguments.
>>> @deco_using_func('func deco') ... def decorated_func1(): ... return >>> decorated_func1...