How does the decorated function of python get the variable or return value of its decorator?

from functools import wraps

def test(func):
    @wraps(func)
    def inner_func():
        inner_obj = "inner_obj"
        print(inner_obj)
        return func()
    return inner_func


@test
def test_func():
    return False

a = test_func ()
print (a)

I want to get the inner_obj variable of the decorator in test_func. Excuse me

Dec.05,2021

of course.

you can add a parameter to test_func. Pass parameters when calling test_func in inner_func

from functools import wraps

def test(func):
    @wraps(func)
    def inner_func():
        inner_obj = 'inner_obj'
        print(inner_obj)
        return func(inner_obj)  -sharp
    return inner_func


@test
def test_func(obj):
    print(obj)      -sharp
    return False

a = test_func()
print(a)

is passed in as a parameter.

  Python decorator: identification criteria for getting started with python  

Menu