The purpose of having a wrapper function is that a function decorator receives a function object to decorate, and it must return the decorated function.
Your 2nd version of my_decorator
doesn't have an explicit return
statement, so it returns None
. When my_decorator
is called via the @
decorator syntax
before some_function() is called.
some fun
after some_function() is called.
gets printed, and then None
gets assigned to the name just_some_fun
. So if you add print(just_some_fun)
to the end of that code it will print None
.
It may be easier to understand what's going on if we get rid of the @
syntactic sugar and re-write your code using normal function calling syntax:
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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…