Python:属性错误- 'NoneType'对象没有属性'something'

时间:2021-12-05 03:02:14

I keep getting an error that says

我不断得到一个错误

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

The code I have is too long to post here, but I was wondering if someone could give a gist of what general scenarios would be cause this 'AttributeError', and what 'NoneType' is supposed to mean? (Usually you would receive the name of some object where the code went wrong, but since it gives me 'NoneType' I'm not sure how it's possible to narrow down what's going on, other than line #)

我的代码太长了,不能在这里发布,但是我想知道是否有人能给出一个大概的场景,因为这个“AttributeError”,什么是“NoneType”?(通常您会收到代码出错的对象的名称,但由于它提供了“NoneType”,因此我不确定除了第#行之外,如何能够缩小范围)

5 个解决方案

#1


183  

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.

NoneType是指你认为你在处理的类或对象的实例,实际上你没有。这通常意味着上面的赋值或函数调用失败或返回意外结果。

#2


73  

You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.

你有一个等于None的变量,你试图访问它的一个属性,叫做" something "。

foo = None
foo.something = 1

or

foo = None
print foo.something

Both will yield an AttributeError: 'NoneType'

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

#3


34  

Others have explained what NoneType is and a common way of ending up with it (i.e., failure to return a value from a function).

其他人解释了NoneType是什么,以及一种常见的结束方式。,无法从函数返回值)。

Another common reason you have None where you don't expect it is assignment of an in-place operation on a mutable object. For example:

另一个常见的原因是您不期望它是对可变对象的就地操作的赋值。例如:

mylist = mylist.sort()

The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you've just assigned None to mylist. If you next try to do, say, mylist.append(1) Python will give you this error.

列表的sort()方法对列表进行排序,即修改mylist。但是该方法的实际返回值是None,而不是排序的列表。所以你没有给mylist分配。如果您接下来尝试做,比如说,mylist.append(1) Python会给出这个错误。

#4


11  

The NoneType is the type of the value None. In this case, the variable lifetime has a value of None.

非etype是没有值的类型。在这种情况下,变量生存期的值为None。

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.

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

#5


7  

Consider the code below.

考虑下面的代码。

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

y = return_something(2)
y.real()

This is going to give you the error

这会得到误差

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

属性错误:'NoneType'对象没有属性'real'

So points are as below.

所以点在下面。

  1. In the code, a function or class method is not returning anything or returning the None
  2. 在代码中,函数或类方法不返回任何内容或不返回任何内容
  3. Then you try to access an attribute of that returned object(which is None), causing the error message.
  4. 然后尝试访问返回的对象的属性(该属性为None),导致错误消息。

#1


183  

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.

NoneType是指你认为你在处理的类或对象的实例,实际上你没有。这通常意味着上面的赋值或函数调用失败或返回意外结果。

#2


73  

You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.

你有一个等于None的变量,你试图访问它的一个属性,叫做" something "。

foo = None
foo.something = 1

or

foo = None
print foo.something

Both will yield an AttributeError: 'NoneType'

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

#3


34  

Others have explained what NoneType is and a common way of ending up with it (i.e., failure to return a value from a function).

其他人解释了NoneType是什么,以及一种常见的结束方式。,无法从函数返回值)。

Another common reason you have None where you don't expect it is assignment of an in-place operation on a mutable object. For example:

另一个常见的原因是您不期望它是对可变对象的就地操作的赋值。例如:

mylist = mylist.sort()

The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you've just assigned None to mylist. If you next try to do, say, mylist.append(1) Python will give you this error.

列表的sort()方法对列表进行排序,即修改mylist。但是该方法的实际返回值是None,而不是排序的列表。所以你没有给mylist分配。如果您接下来尝试做,比如说,mylist.append(1) Python会给出这个错误。

#4


11  

The NoneType is the type of the value None. In this case, the variable lifetime has a value of None.

非etype是没有值的类型。在这种情况下,变量生存期的值为None。

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.

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

#5


7  

Consider the code below.

考虑下面的代码。

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

y = return_something(2)
y.real()

This is going to give you the error

这会得到误差

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

属性错误:'NoneType'对象没有属性'real'

So points are as below.

所以点在下面。

  1. In the code, a function or class method is not returning anything or returning the None
  2. 在代码中,函数或类方法不返回任何内容或不返回任何内容
  3. Then you try to access an attribute of that returned object(which is None), causing the error message.
  4. 然后尝试访问返回的对象的属性(该属性为None),导致错误消息。