如何修复装饰器中留下的非类型错误
how to fix nonetype error leaving in decorators
错误问题“创建了字典名称user2,但运行程序出错。此程序是对已传递给用户的消息进行身份验证。”
代码如下
user2 = {
'name':'parth',
'valid':True
}
def authentication(func):
def wrap(*args, **kwargs):
if args[0]['valid']:
return func(*args, **kwargs)
return wrap
@authentication
def message_sent(user):
print('The message has been delivered')
message_sent(user2)
输出为
Traceback (most recent call last):
File "someFileName", line 16, in <module>
message_sent(user2)
TypeError: 'NoneType' object is not callable
return wrap
缩进了一级。
您没有在身份验证函数中返回任何内容,这是错误的主要原因。您刚刚返回了 wrap inside wrap 函数。所以 outdent 是你需要的
修改认证函数如下:
def authentication(func):
def wrap(*args, **kwargs):
if args[0]['valid']:
return func(*args, **kwargs)
return wrap
错误问题“创建了字典名称user2,但运行程序出错。此程序是对已传递给用户的消息进行身份验证。”
代码如下
user2 = {
'name':'parth',
'valid':True
}
def authentication(func):
def wrap(*args, **kwargs):
if args[0]['valid']:
return func(*args, **kwargs)
return wrap
@authentication
def message_sent(user):
print('The message has been delivered')
message_sent(user2)
输出为
Traceback (most recent call last):
File "someFileName", line 16, in <module>
message_sent(user2)
TypeError: 'NoneType' object is not callable
return wrap
缩进了一级。
您没有在身份验证函数中返回任何内容,这是错误的主要原因。您刚刚返回了 wrap inside wrap 函数。所以 outdent 是你需要的
修改认证函数如下:
def authentication(func):
def wrap(*args, **kwargs):
if args[0]['valid']:
return func(*args, **kwargs)
return wrap