はいそうです。ご存知のように、デコレータは関数です。形式で書かれた場合:
def mydecorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@mydecorator
def foo(a, b, c):
pass
mydecorator
に渡される引数は、関数foo
そのものです。
デコレータが引数を受け入れると、@mydecorator('/path/to')
という呼出しは、実際には '/ path/to'でmydecorator関数を呼び出すことになります。その後、を受け取るためにmydecorator(path)
への呼び出しの結果が呼び出されます。動的ラッパー関数を効果的に定義しています。
一言で言えば、デコレータ機能の別のレイヤーが必要です。ここで
はこの少し愚かな例です:ポールの答えは良いです
def addint(val):
def decorator(func):
def wrapped(*args, **kwargs):
result = func(*args, **kwargs)
return result + val
return wrapped # returns the decorated function "add_together"
return decorator # returns the definition of the decorator "addint"
# specifically built to return an extra 5 to the sum
@addint(5)
def add_together(a, b):
return a + b
print add_together(1, 2)
# prints 8, not 3