我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
我一直得到一个错误,说
AttributeError: 'NoneType' object has no attribute 'something'
我的代码太长了,不能在这里发布。什么样的场景会导致这个AttributeError, NoneType是什么意思,我该如何缩小范围?
当前回答
“NoneType”为None值的类型。在本例中,变量生命周期的值为None。
实现这种情况的常见方法是调用一个缺少返回值的函数。
然而,还有无数种其他方法可以将变量设置为None。
其他回答
如果在Flask应用程序中注释掉HTML,就会出现这个错误。这里qual.date_expiry的值是None:
<!-- <td>{{ qual.date_expiry.date() }}</td> -->
删除或修复这一行:
<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
“NoneType”为None值的类型。在本例中,变量生命周期的值为None。
实现这种情况的常见方法是调用一个缺少返回值的函数。
然而,还有无数种其他方法可以将变量设置为None。
NoneType意味着不是你认为你正在处理的任何类或对象的实例,你实际上得到的是None。这通常意味着上面的赋值或函数调用失败或返回意外结果。
在构建估计器(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函数中可以修复此错误。
if val is not None:
print(val)
else:
# no need for else: really if it doesn't contain anything useful
pass
检查特定数据是否为空或空。