为什么我会收到 AttributeError: 'NoneType' object has no attribute 'something'?

Why do I get AttributeError: 'NoneType' object has no attribute 'something'?

提问人:Jacob Griffin 提问时间:1/21/2012 最后编辑:Karl KnechtelJacob Griffin 更新时间:3/27/2023 访问量:1821725

问:

我收到一条错误消息,内容如下:

AttributeError: 'NoneType' object has no attribute 'something'

我该如何理解此消息?

哪些一般情况可能导致这样的情况,我该如何识别问题?AttributeError


这是 AttributeErrors 的特例。它值得单独处理,因为有很多方法可以从代码中获取意外的 None 值,因此这通常是一个不同的问题;对于其他 AttributeErrors,问题可能同样容易出在属性名称上。

另请参阅什么是 None 值?和什么是“NoneType”对象?,以了解 None 及其类型 NoneType

python 属性错误 nonetype

评论

2赞 Karl Knechtel 3/26/2023
TODO:为 的一般情况找到一个合适的规范,以包含在 see-also 部分。stackoverflow.com/questions/11685936 不合适,因为它是由具有 2.x 特定效果的拼写错误(混合制表符/空格缩进)引起的,这会导致答案中的混淆和不准确。AttributeError

答:

440赞 g.d.d.c 1/21/2012 #1

NoneType 意味着,您实际上拥有的不是您认为正在使用的任何类或对象的实例,而是 .这通常意味着上面的赋值或函数调用失败或返回意外结果。None

评论

3赞 Shrout1 10/28/2022
记住从函数调用中实际返回也很有帮助......尝试引用未返回的项目通常效果不太好。傻傻的我。
0赞 rahulspoudel 1/22/2023
这对我来说是确切的问题。我经常回到这里。:D谢谢。
140赞 koblas 1/21/2012 #2

您有一个等于 None 的变量,并且您正在尝试访问该变量的一个名为“something”的属性。

foo = None
foo.something = 1

foo = None
print(foo.something)

两者都将产生一个AttributeError: 'NoneType'

评论

21赞 tripleee 2/4/2019
这可能是无济于事的,除非你指出人们最终可能会如何从某事中得到好处。显式不太可能是问题;这将是,当它不成功或结果集为空或其他什么时,您不会意识到可能会返回。Nonefoo = Nonefoo = something()something()None
0赞 Adeolu Temitope Olofintuyi 8/27/2021
这是完全正确的。例如,当你使用 Django 开发一个电子商务应用程序时,你已经研究了购物车的功能,当你用产品测试购物车功能时,一切似乎都正常。然后在后端删除已注册到购物车的产品。如果您再次尝试转到购物车页面,您将遇到上述错误。必须进行适当的修复以避免这种情况。
17赞 S.Lott 1/21/2012 #3

是值的类型。在本例中,变量的值为 。NoneTypeNonelifetimeNone

发生这种情况的一种常见方法是调用缺少 .return

但是,有无数种其他方法可以将变量设置为 None。

评论

1赞 g.d.d.c 11/14/2014
我不认为有(预编辑)的值。他试图访问其他东西的生命周期属性lifetimeNoneNone
15赞 PHINCY L PIOUS 4/10/2017 #4

请考虑下面的代码。

def return_something(someint):
 if  someint > 5:
    return someint

y = return_something(2)
y.real()

这将给你带来错误

AttributeError:“NoneType”对象没有属性“real”

所以要点如下。

  1. 在代码中,函数或类方法不返回任何内容或返回 None
  2. 然后,您尝试访问返回对象的属性(即 None),从而导致错误消息。
1赞 barribow 2/19/2019 #5

G.D.D.C. 是对的,但添加一个非常常见的例子:

您可以以递归形式调用此函数。在这种情况下,最终可能会出现 null 指针或 。在这种情况下,您可能会收到此错误。因此,在访问该参数的属性之前,请检查它是否不是 .NoneTypeNoneType

评论

0赞 not2qubit 10/16/2019
是的,好的,但是你如何进行检查呢?
0赞 Gringo Suave 11/6/2019
if foo is not None:
0赞 tripleee 5/6/2023
诚然,在每种情况下忘记某些东西是递归函数的常见错误;但解释是,这个答案似乎不够详细,无法真正有用。请参阅常见的常见问题解答 stackoverflow.com/questions/17778372/...return
4赞 M. Hamza Rajput 3/3/2019 #6

它表示您尝试访问的对象。 是 Python 中的一个变量。 这种类型的错误是 de 你的代码是这样的。NoneNoneNull

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")
0赞 Jeremy Thompson 10/23/2019 #7

您可能会在 Flask 应用程序中注释掉 HTML 时出现此错误。此处 qual.date_expiry 的值为 None:

   <!-- <td>{{ qual.date_expiry.date() }}</td> -->

删除该行或修复它:

<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
3赞 Chiel 1/29/2020 #8

在构建估计器 (sklearn) 时,如果忘记在拟合函数中返回 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”对象没有属性“transform”?

添加到拟合函数可修复错误。return self

评论

1赞 tripleee 4/7/2022
也许值得指出的是,没有显式任何东西的函数将隐含。如果你的函数基本上是,那么当不真实时,它最终会陷入隐含的边缘,这也是这种情况。(PHINCY L PIOUS的回答详细阐述了这个特殊情况。returnreturn Noneif something: return valuereturn Nonesomething
7赞 Shah Vipul 12/21/2020 #9
if val is not None:
    print(val)
else:
    # no need for else: really if it doesn't contain anything useful
    pass

检查特定数据是否为空或 null。

评论

1赞 tripleee 11/6/2022
else: pass完全没用;如果你没有任何东西要放进去,只需省略它。else:
-1赞 David Newcomb 7/22/2022 #10

这里的其他答案都没有给我正确的解决方案。我遇到过这种情况:

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==is

if answer is None:
   print('Empty')
else:
   print('Not empty')

评论

1赞 tripleee 11/6/2022
其中一个教训是认真思考什么时候产生的问题多于它解决的问题。在某些情况下,提出异常可能更有意义。return None