Python: Attribute Error - 'NoneType' object has no attribute 'something'

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34355232/article/details/84025575

很多人使用python的时候,都会遇到’NoneType’ object has no attribute ××××××这样的问题。
出现这样问题的原因是:

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

foo = None
foo.something = 1
或
foo = None
print foo.something

以上两种都会报Nonetype’ object has no attribute ××××××这样的问题,所以根据这个我们可以看出来 ,其实就是foo你以为有值,但是其什么都没有,从而导致这样的问题。

解决方法:
检查你的变量是否为空。

参考链接:https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something

猜你喜欢

转载自blog.csdn.net/qq_34355232/article/details/84025575