Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤問(wèn)題解決方案
AttributeError: ‘NoneType’ object has no attribute ‘X’ 介紹
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問(wèn) None 值的屬性時(shí),例如 來(lái)自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯(cuò)誤,請(qǐng)?jiān)谠L問(wèn)屬性之前更正分配。
這是一個(gè)非常簡(jiǎn)單的示例,說(shuō)明錯(cuò)誤是如何發(fā)生的。
example = None
# ?? AttributeError: 'NoneType' object has no attribute 'append'
example.append('hello')
嘗試訪問(wèn)或設(shè)置 None 值的屬性會(huì)導(dǎo)致錯(cuò)誤。
如果打印正在訪問(wèn)其屬性的變量,它將為 None,因此您必須追蹤變量被分配 None 值的位置并更正分配。
None 值(顯式賦值除外)的最常見(jiàn)來(lái)源是不返回任何內(nèi)容的函數(shù)。
# ??? this function returns None
def do_math(a, b):
print(a * b)
# ??? None
example = do_math(10, 10)
# ?? AttributeError: 'NoneType' object has no attribute 'my_attribute'
print(example.my_attribute)
# ? use this if you need to check if a variable is not None
# before accessing an attribute
if example is not None:
print('variable is not None')
else:
print('variable is None')
請(qǐng)注意,我們的do_math函數(shù)沒(méi)有顯式返回值,因此它隱式返回 None。
我們將調(diào)用函數(shù)的結(jié)果分配給一個(gè)變量,并試圖訪問(wèn)導(dǎo)致錯(cuò)誤的屬性。
AttributeError: ‘NoneType’ object has no attribute ‘X’ 常見(jiàn)原因
“AttributeError: ‘NoneType’ object has no attribute” 的發(fā)生有多種原因:
- 具有不返回任何內(nèi)容的函數(shù)(隱式返回
None)。 - 將變量顯式設(shè)置為
None。 - 將變量分配給調(diào)用不返回任何內(nèi)容的內(nèi)置函數(shù)的結(jié)果。
- 具有僅在滿(mǎn)足特定條件時(shí)才返回值的函數(shù)。
一些內(nèi)置方法(例如排序)會(huì)就地改變數(shù)據(jù)結(jié)構(gòu)并且不返回值。 換句話說(shuō),它們隱式返回 None。
my_list = ['c', 'b', 'a'] my_sorted_list = my_list.sort() print(my_sorted_list) # ??? None # ?? AttributeError: 'NoneType' object has no attribute 'pop' my_sorted_list.pop()
sort() 方法對(duì)列表進(jìn)行就地排序并且不返回任何內(nèi)容,因此當(dāng)我們將調(diào)用該方法的結(jié)果分配給變量時(shí),我們?yōu)樵撟兞糠峙淞艘粋€(gè) None 值。
避免使用會(huì)改變?cè)紝?duì)象的方法進(jìn)行賦值,因?yàn)樗鼈冎械拇蠖鄶?shù)不返回值并隱式返回
None。
如果一個(gè)變量有時(shí)存儲(chǔ)一個(gè)真實(shí)值,有時(shí)存儲(chǔ) None ,則可以在訪問(wèn)屬性之前明確檢查該變量是否不是 None 。
example = None
if example is not None:
print('variable is not None')
else:
print('variable is None') # ??? this runs僅當(dāng)示例變量未存儲(chǔ) None 值時(shí), if 塊才會(huì)運(yùn)行,否則運(yùn)行 else 塊。
錯(cuò)誤的另一個(gè)常見(jiàn)原因是具有僅在滿(mǎn)足條件時(shí)才返回值的函數(shù)。
def get_num(a):
if a > 100:
return a
result = get_num(5)
print(result) # ??? Noneget_num 函數(shù)中的 if 語(yǔ)句僅在傳入的參數(shù)大于 100 時(shí)運(yùn)行。
在所有其他情況下,該函數(shù)不返回任何內(nèi)容并最終隱式返回
None。
AttributeError: ‘NoneType’ object has no attribute ‘X’ 錯(cuò)誤解決方法
要解決這種情況下的錯(cuò)誤,我們要么必須檢查函數(shù)是否未返回 None ,要么在不滿(mǎn)足條件時(shí)返回默認(rèn)值。
def get_num(a):
if a > 100:
return a
return 0 # ??? returns 0 if condition not met
result = get_num(5)
print(result) # ??? 0現(xiàn)在,無(wú)論條件是否滿(mǎn)足,函數(shù)都保證返回一個(gè)值。
總結(jié)
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問(wèn) None 值的屬性時(shí),例如 來(lái)自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯(cuò)誤,請(qǐng)?jiān)谠L問(wèn)屬性之前更正分配。
到此這篇關(guān)于Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤的文章就介紹到這了,更多相關(guān)Python 中 AttributeError內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python錯(cuò)誤:AttributeError: ''module'' object has no attribute ''setdefaultencoding''問(wèn)題的解決方法
- 解決python多線程報(bào)錯(cuò):AttributeError: Can''t pickle local object問(wèn)題
- Python3下錯(cuò)誤AttributeError: ‘dict’ object has no attribute’iteritems‘的分析與解決
- 解決AttributeError:'NoneTypeobject'?has?no?attribute'Window'的問(wèn)題(親測(cè)有效)
- Python進(jìn)程崩潰AttributeError異常問(wèn)題解決
- Pytorch出現(xiàn)錯(cuò)誤Attribute?Error:module?‘torch‘?has?no?attribute?'_six'解決
相關(guān)文章
pytorch實(shí)現(xiàn)查看當(dāng)前學(xué)習(xí)率
這篇文章主要介紹了pytorch實(shí)現(xiàn)查看當(dāng)前學(xué)習(xí)率,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Pytorch中的自動(dòng)求梯度機(jī)制和Variable類(lèi)實(shí)例
今天小編就為大家分享一篇Pytorch中的自動(dòng)求梯度機(jī)制和Variable類(lèi)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
關(guān)于PyTorch源碼解讀之torchvision.models
今天小編就為大家分享一篇關(guān)于PyTorch源碼解讀之torchvision.models,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python?peewee操作sqlite鎖表的問(wèn)題分析
Peewee是一種簡(jiǎn)單而小的ORM,在使用python?orm?框架?peewee?操作數(shù)據(jù)庫(kù)時(shí)時(shí)常會(huì)拋出以一個(gè)異常,下面我們就來(lái)分享一下具體的原因以及解決辦法吧2023-08-08
Python第三方庫(kù)jieba庫(kù)與中文分詞全面詳解
jieba庫(kù)是一款優(yōu)秀的Python第三方中文分詞庫(kù),jieba支持三種分詞模式:精確模式、全模式和搜索引擎模式,下面這篇文章主要給大家介紹了關(guān)于Python第三方庫(kù)jieba庫(kù)與中文分詞的相關(guān)資料,需要的朋友可以參考下2022-07-07

