我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
当前回答
如果在Flask应用程序中注释掉HTML,就会出现这个错误。这里qual.date_expiry的值是None:
<!-- <td>{{ qual.date_expiry.date() }}</td> -->
删除或修复这一行:
<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
其他回答
if val is not None:
print(val)
else:
# no need for else: really if it doesn't contain anything useful
pass
检查特定数据是否为空或空。
如果在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的变量,你试图访问它的一个名为“something”的属性。
foo = None
foo.something = 1
or
foo = None
print(foo.something)
两者都会产生一个AttributeError: 'NoneType'
在构建估计器(sklearn)时,如果您忘记在fit函数中返回self,则会得到相同的错误。
class ImputeLags(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
def fit(self, x, y=None):
""" do something """
def transfrom(self, x):
return x
AttributeError:“NoneType”对象没有属性“转换”?
将return self添加到fit函数中可以修复此错误。
考虑下面的代码。
def return_something(someint):
if someint > 5:
return someint
y = return_something(2)
y.real()
这就会给出误差
AttributeError: 'NoneType'对象没有'real'属性
点如下所示。
在代码中,函数或类方法不返回任何东西或返回None 然后,您尝试访问该返回对象的属性(该属性为None),从而导致错误消息。