为什么我会收到 AttributeError: 'NoneType'对象没有属性“某物”?
我收到一条错误消息,内容为
AttributeError: 'NoneType' object has no attribute 'something'
我如何理解此消息?
哪些一般情况可能会导致此类 AttributeError
,以及如何识别问题?
这是AttributeError
的特例。它值得单独处理,因为有很多方法可以从代码中获取意外的 None
值,因此这通常是一个不同的问题;对于其他 AttributeError
,问题可能很容易出在属性名称上。
另请参阅什么是 None 值? 和什么是“NoneType”对象? 了解 None< /code> 及其类型,
NoneType
。
I am getting an error message that says
AttributeError: 'NoneType' object has no attribute 'something'
How can I understand this message?
What general scenarios might cause such an AttributeError
, and how can I identify the problem?
This is a special case of AttributeError
s. It merits separate treatment because there are a lot of ways to get an unexpected None
value from the code, so it's typically a different problem; for other AttributeError
s, the problem might just as easily be the attribute name.
See also What is a None value? and What is a 'NoneType' object? for an understanding of None
and its type, NoneType
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
NoneType 意味着您实际上拥有的是
None
,而不是您认为正在使用的任何类或对象的实例。这通常意味着上面的赋值或函数调用失败或返回意外结果。NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got
None
. That usually means that an assignment or function call up above failed or returned an unexpected result.您有一个等于 None 的变量,并且您正在尝试访问它的一个名为“something”的属性。
或者
两者都会产生
AttributeError: 'NoneType'
You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.
or
Both will yield an
AttributeError: 'NoneType'
NoneType
是值None
的类型。在本例中,变量lifetime
的值为None
。发生这种情况的常见方法是调用缺少
返回
的函数。但是,还有无数其他方法可以将变量设置为 None。
The
NoneType
is the type of the valueNone
. In this case, the variablelifetime
has a value ofNone
.A common way to have this happen is to call a function missing a
return
.There are an infinite number of other ways to set a variable to None, however.
考虑下面的代码。
这会给你错误
所以要点如下。
Consider the code below.
This is going to give you the error
So points are as below.
检查特定数据是否不为空或为空。
Check whether particular data is not empty or null.
它表示您尝试访问的对象
None
。None
是 python 中的一个Null
变量。这种类型的错误发生在您的代码中,类似于这样。
It means the object you are trying to access
None
.None
is aNull
variable in python.This type of error is occure de to your code is something like this.
获取
None
的另一个常见原因是错误地将方法的返回值分配给所操作的对象。这里的问题是
.sort
方法就地修改了列表,并返回None
。您需要或者
(在这种情况下,前者可能是首选。)
还有其他几种具有类似签名的方法。
sort
是一种常见的方法,但这只是其中的一个示例。此信息有一个中等高度赞成的答案,但其作者删除了它。我添加了一个新答案,以使讨论保持合理完整。
Another common cause of getting
None
is to incorrectly assign a method's return value to the manipulated object.The problem here is that the
.sort
method modifies the list in place, and returnsNone
. You want eitheror
(The former should probably be preferred in this scenario.)
There are several other methods with a similar signature.
sort
is a common one, but it's just one example of this.There was a moderately highly upvoted answer with this information, but its author deleted it. I'm adding a new answer to keep the discussion reasonably complete.
在构建估计器(sklearn)时,如果您忘记在 fit 函数中返回 self ,您会得到相同的错误。
将
return self
添加到 fit 函数可修复该错误。When building a estimator (sklearn), if you forget to return self in the fit function, you get the same error.
Adding
return self
to the fit function fixes the error.该错误意味着您正在尝试访问
None
的属性或方法,但NoneType
没有任何属性或方法。当您调用不返回任何内容的函数时,可能会发生这种情况,导致其返回值为None
。示例:发生这种情况是因为内置函数
exec
没有返回值,而我们尝试在None
上调用 lower 方法。The error means that you are trying to access an attribute or method of
None
, butNoneType
does not have any. This can happen when you call a function that does not return anything, which results in its return value isNone
. Example:This happens because the built-in function
exec
does not return a value, and we are trying to call the lower method onNone
.gddc 是对的,但添加一个非常常见的示例:
您可以以递归形式调用此函数。在这种情况下,您可能会得到空指针或
NoneType
。在这种情况下,您可能会收到此错误。因此,在访问该参数的属性之前,请检查它是否不是NoneType
。g.d.d.c. is right, but adding a very frequent example:
You might call this function in a recursive form. In that case, you might end up at null pointer or
NoneType
. In that case, you can get this error. So before accessing an attribute of that parameter check if it's notNoneType
.这里的其他答案都没有给我正确的解决方案。我遇到了这种情况:
错误如下:
在这种情况下,您无法使用
==
测试与None
的相等性。为了修复它,我将其更改为使用is
代替:None of the other answers here gave me the correct solution. I had this scenario:
Which errored with:
In this case you can't test equality to
None
with==
. To fix it I changed it to useis
instead: