我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
当前回答
NoneType意味着不是你认为你正在处理的任何类或对象的实例,你实际上得到的是None。这通常意味着上面的赋值或函数调用失败或返回意外结果。
其他回答
NoneType意味着不是你认为你正在处理的任何类或对象的实例,你实际上得到的是None。这通常意味着上面的赋值或函数调用失败或返回意外结果。
这里没有一个答案是正确的。我有这样一个场景:
def my_method():
if condition == 'whatever':
....
return 'something'
else:
return None
answer = my_method()
if answer == None:
print('Empty')
else:
print('Not empty')
错误如下:
File "/usr/local/lib/python3.9/site-packages/gitlab/base.py", line 105, in __eq__
if self.get_id() and other.get_id():
AttributeError: 'NoneType' object has no attribute 'get_id'
在这种情况下,你不能用==测试是否等于None。为了修复它,我把它改为使用:
if answer is None:
print('Empty')
else:
print('Not empty')
如果在Flask应用程序中注释掉HTML,就会出现这个错误。这里qual.date_expiry的值是None:
<!-- <td>{{ qual.date_expiry.date() }}</td> -->
删除或修复这一行:
<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
它表示您试图访问的对象为None。None是python中的Null变量。 这种类型的错误发生在你的代码是这样的。
x1 = None
print(x1.something)
#or
x1 = None
x1.someother = "Hellow world"
#or
x1 = None
x1.some_func()
# you can avoid some of these error by adding this kind of check
if(x1 is not None):
... Do something here
else:
print("X1 variable is Null or None")
你有一个等于None的变量,你试图访问它的一个名为“something”的属性。
foo = None
foo.something = 1
or
foo = None
print(foo.something)
两者都会产生一个AttributeError: 'NoneType'