You can't pass it as its own name, but you can add it to the keywords.
def decorate(function):
def wrap_function(*args, **kwargs):
kwargs['str'] = 'Hello!'
return function(*args, **kwargs)
return wrap_function
@decorate
def print_message(*args, **kwargs):
print(kwargs['str'])
Alternatively you can name its own argument:
def decorate(function):
def wrap_function(*args, **kwargs):
str = 'Hello!'
return function(str, *args, **kwargs)
return wrap_function
@decorate
def print_message(str, *args, **kwargs):
print(str)
Class method:
def decorate(function):
def wrap_function(*args, **kwargs):
str = 'Hello!'
args.insert(1, str)
return function(*args, **kwargs)
return wrap_function
class Printer:
@decorate
def print_message(self, str, *args, **kwargs):
print(str)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…