问题:为什么会出现AttributeError:’NoneType’对象没有属性’something’?

我不断收到错误消息,说

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

我的代码太长,无法在此处发布。什么一般情况会导致这种情况AttributeError,这NoneType意味着什么,我如何缩小正在发生的事情?

I keep getting an error that says

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

The code I have is too long to post here. What general scenarios would cause this AttributeError, what is NoneType supposed to mean and how can I narrow down what’s going on?


回答 0

NoneType意味着您实际上拥有了而不是您认为正在使用的任何Class或Object的实例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.


回答 1

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

foo = None
foo.something = 1

要么

foo = None
print foo.something

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

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

foo = None
foo.something = 1

or

foo = None
print(foo.something)

Both will yield an AttributeError: 'NoneType'


回答 2

其他人则解释了什么NoneType是结束它的常见方法(即,无法从函数返回值)。

另一个None不希望看到的常见原因是在可变对象上分配了就地操作。例如:

mylist = mylist.sort()

sort()列表的方法对列表进行原位排序,mylist即被修改。但是该方法的实际返回值None不是对列表进行排序。因此,您刚刚分配Nonemylist。如果您下次尝试这样做,mylist.append(1)Python会给您这个错误。

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).

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.


回答 3

NoneType是该值的类型None。在这种情况下,变量lifetime的值为None

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

但是,还有无数其他方法可以将变量设置为“无”。

The NoneType is the type of the value None. In this case, the variable lifetime has a value of 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.


回答 4

考虑下面的代码。

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

y = return_something(2)
y.real()

这会给你错误

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

所以要点如下。

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

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’

So points are as below.

  1. In the code, a function or class method is not returning anything or returning the None
  2. Then you try to access an attribute of that returned object(which is None), causing the error message.

回答 5

这意味着您正在尝试访问的对象NoneNoneNullpython中的变量。这种类型的错误发生在您的代码上,就像这样。

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")

It means the object you are trying to access None. None is a Null variable in python. This type of error is occure de to your code is something like this.

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")

回答 6

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 not NoneType.


回答 7

建立估算器(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’对象没有属性’transform’?

添加return self到拟合功能可修复该错误。

When building a estimator (sklearn), if you forget to return self in the fit function, you get the same error.

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’ object has no attribute ‘transform’?

Adding return self to the fit function fixes the error.


回答 8

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

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

删除行或修复它:

<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>

You can get this error with you have commented out HTML in a Flask application. Here the value for qual.date_expiry is None:

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

Delete the line or fix it up:

<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>

回答 9

如果我们分配如下所示的内容,则会引发错误,例如“ AttributeError:’NoneType’对象没有属性’show’”

df1=df.withColumn('newAge',df['Age']).show() 

if we assign something like the below, it will throw error as “AttributeError: ‘NoneType’ object has no attribute ‘show'”

df1=df.withColumn('newAge',df['Age']).show() 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。