如何将装饰器中的变量传递给装饰函数中的函数参数?

时间:2023-02-14 21:02:29

I am in progress to learn Python. Hopefully someone points me to correct way.
This is what I'd like to do below:

我正在学习Python。希望有人指出我正确的方式。这就是我想在下面做的事情:

def decorate(function):
    def wrap_function(*args, **kwargs):
        str = 'Hello!'  # This is what I want
        return function(*args, **kwargs)
    return wrap_function

@decorate
def print_message():
    # I'd like to pass 'str' as mentioned above
    # to any functions' argument like below:
    print(str)  # 'str' is same as above

Any idea? Thanks in advance.

任何的想法?提前致谢。

2 个解决方案

#1


33  

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)

#2


7  

If you want the argument to be "optionally-injected", only in case the function actually takes it, use something like this:

如果你想让参数“可选地注入”,只有在函数实际接受它的情况下,使用类似这样的东西:

import inspect

def decorate(func):
    def wrap_and_call(*args, **kwargs):
        if 'str' in inspect.getargspec(func).args:
            kwargs['str'] = 'Hello!'
        return func(*args, **kwargs)
    return wrap_and_call

@decorate
def func1(str):
    print "Works! - " + str

@decorate
def func2():
    print "Should work, also."

#1


33  

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)

#2


7  

If you want the argument to be "optionally-injected", only in case the function actually takes it, use something like this:

如果你想让参数“可选地注入”,只有在函数实际接受它的情况下,使用类似这样的东西:

import inspect

def decorate(func):
    def wrap_and_call(*args, **kwargs):
        if 'str' in inspect.getargspec(func).args:
            kwargs['str'] = 'Hello!'
        return func(*args, **kwargs)
    return wrap_and_call

@decorate
def func1(str):
    print "Works! - " + str

@decorate
def func2():
    print "Should work, also."