问题描述
这是一个用于创建装饰器的python程序 今天,我第一次使用stackoverflow ...所以我在上传此问题时遇到问题
#The code-
def maint(item1):
def greet():
print("Good Morning")
item1()
print("Tanish")
return greet()
#decorator----
@maint
def hello():
print("Hello")
# hello=maint(hello)
hello()
这是一个用于创建装饰器的python程序
解决方法
return greet()
在装饰器中,调用greet()
并返回其结果。由于greet()
没有明确的回报,因此结果为None
。这将有助于认识到装饰器是类似以下内容的简写语法:
def hello():
pass
hello = maint(hello)
请注意,如何将您好重新分配给maint()
返回的值。在您的情况下,hello
被重新分配给None
。因此,调用hello()
会导致错误。
要解决此问题,只需return greet
,不带括号。装饰器总是返回一个函数。他们不应该调用该函数。