如果我创建一个装饰像以下:为什么我们需要装饰器中的包装函数?
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!")
另一种装饰,可以定义为:
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")
装饰都将工作一样。在装饰器中使用“包装器”功能有什么好处。我没有达到目的。
你尝试过吗?这两个代码块对我来说产生完全不同的结果。 – RishiG
真的吗?你有没有尝试在你的第二代码块中调用'just_some_fun()'?是的,当'my_decorator'在'just_some_fun'上被调用,但是当_call_'just_some_fun()'它与'TypeError:'NoneType'对象不可调用'崩溃时,东西被打印出来。 –
“两个装饰者都会一样” - 你有没有支持这个说法? – user2357112