我知道,如果我们不返回任何内容,那么返回的变量将一无所有。 但是当我看装饰器示例时:
import time
def timing_function(some_function):
"""
Outputs the time a function takes
to execute.
"""
def wrapper():
t1 = time.time()
some_function()
t2 = time.time()
return "Time it took to run the function: " + str((t2 - t1)) + "\n"
return wrapper
对于第一个返回,我们是否返回“运行函数所花费的时间:” + str((t2-t1))+“ \ n”该值返回包装函数,然后在第二个返回时,返回包装值,这是“再次运行timing_function的函数运行时间:” + str((t2-t1))+“ \ n”?
然后这段代码怎么样:
def my_decorator(some_function):
def wrapper():
num = 10
if num == 10:
print("Yes!")
else:
print("No!")
some_function()
print("Something is happening after some_function() is called.")
return wrapper
什么以及为什么我们要返回wrapper()?还是什么都没有返回wrapper(),但是我们将包装返回了my_decorator()呢?