Why do we need a wrapper function in decorators?

If I create a decorator as shown below:

def my_decorator(some_fun):
    def wrapper():
        print("before some_function() is called.")
        some_fun()
        print("after some_function() is called.")
    return wrapper

@my_decorator
def just_some_function():
    print("Wheee!")

Another decorator can be defined as:

def my_decorator(some_fun):
    print("before some_function() is called.")
    some_fun()
    print("after some_function() is called.")

@my_decorator
def just_some_fun():
    print("some fun")

Both decorators will work the same way. What is the advantage of using the wrapper function inside the decorator. I did not get this goal.

+7
source share
3 answers

The purpose of the wrapper function is that the function decorator receives the function object for decoration, and should return the decorated function.

There is my_decoratorno explicit statement in your second version return, so it returns None. When my_decoratorcalled using @decorator syntax

before some_function() is called.
some fun
after some_function() is called.

, None just_some_fun. , print(just_some_fun) , None.

, , @ :

def my_decorator(some_fun):
    print("before some_function() is called.")
    some_fun()
    print("after some_function() is called.")

def just_some_fun():
    print("some fun")

just_some_fun = my_decorator(just_some_fun)
+4

Python , , , . , (, , ). , .

Python , Python , , ( ), .

, , .

, :

@my_decorator
def my_function()
  print("My_function")

:

def my_function()
    print("My_function")    

my_function = my_decorator(my_function)

def my_decorator(func):
    print("decorated)
    return 42

my_function , ( print(my_function))

,

def my_decorator2(some_fun):
    print("before")
    some_fun()
    print("after")

( python , None).

@my_decorator2
def decorated():
  print("inside")

before
inside
after

decorated() 'NoneType' object is not callable, decorated None.

, - , ( "wrap" ). - , /, - .

+2

, -, , , -.

1

, None pass

def decorator_func(to_be_decorated_function):
    print("Logging IN: Currrently  in function")
    to_be_decorated_function()
    print("Logging OUT: Currrently  in function")

    def a(): None  # or def a(): pass

    return (a)

@decorator_func
def to_be_decorated_function():
    print('October 16, 2000')

to_be_decorated_function()
# equivalent to 
#to_be_decorated_function = decorator_func(to_be_decorated_function)

2

. , .

def decorator_func(to_be_decorated_function):
    print("Logging IN: Currrently  in function")
    to_be_decorated_function()
    print("Logging OUT: Currrently  in function")

@decorator_func
def to_be_decorated_function():
    print('October 16, 2000')

to_be_decorated_function  # notice I'm just using the object and not callable function
# equivalent to
#decorator_func(to_be_decorated_function)

Logging IN: Currrently  in function
October 16, 2000
Logging OUT: Currrently  in function
0

Source: https://habr.com/ru/post/1682341/


All Articles