Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤問題解決方案
AttributeError: ‘NoneType’ object has no attribute ‘X’ 介紹
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問 None 值的屬性時(shí),例如 來自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯(cuò)誤,請?jiān)谠L問屬性之前更正分配。
這是一個(gè)非常簡單的示例,說明錯(cuò)誤是如何發(fā)生的。
example = None # ?? AttributeError: 'NoneType' object has no attribute 'append' example.append('hello')
嘗試訪問或設(shè)置 None 值的屬性會導(dǎo)致錯(cuò)誤。
如果打印正在訪問其屬性的變量,它將為 None,因此您必須追蹤變量被分配 None 值的位置并更正分配。
None 值(顯式賦值除外)的最常見來源是不返回任何內(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')
請注意
,我們的do_math
函數(shù)沒有顯式返回值,因此它隱式返回 None。
我們將調(diào)用函數(shù)的結(jié)果分配給一個(gè)變量,并試圖訪問導(dǎo)致錯(cuò)誤的屬性。
AttributeError: ‘NoneType’ object has no attribute ‘X’ 常見原因
“AttributeError: ‘NoneType’ object has no attribute” 的發(fā)生有多種原因:
- 具有不返回任何內(nèi)容的函數(shù)(隱式返回
None
)。 - 將變量顯式設(shè)置為
None
。 - 將變量分配給調(diào)用不返回任何內(nèi)容的內(nèi)置函數(shù)的結(jié)果。
- 具有僅在滿足特定條件時(shí)才返回值的函數(shù)。
一些內(nèi)置方法(例如排序)會就地改變數(shù)據(jù)結(jié)構(gòu)并且不返回值。 換句話說,它們隱式返回 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()
方法對列表進(jìn)行就地排序并且不返回任何內(nèi)容,因此當(dāng)我們將調(diào)用該方法的結(jié)果分配給變量時(shí),我們?yōu)樵撟兞糠峙淞艘粋€(gè) None 值。
避免使用會改變原始對象的方法進(jìn)行賦值,因?yàn)樗鼈冎械拇蠖鄶?shù)不返回值并隱式返回
None
。
如果一個(gè)變量有時(shí)存儲一個(gè)真實(shí)值,有時(shí)存儲 None
,則可以在訪問屬性之前明確檢查該變量是否不是 None
。
example = None if example is not None: print('variable is not None') else: print('variable is None') # ??? this runs
僅當(dāng)示例變量未存儲 None
值時(shí), if
塊才會運(yùn)行,否則運(yùn)行 else
塊。
錯(cuò)誤的另一個(gè)常見原因是具有僅在滿足條件時(shí)才返回值的函數(shù)。
def get_num(a): if a > 100: return a result = get_num(5) print(result) # ??? None
get_num
函數(shù)中的 if
語句僅在傳入的參數(shù)大于 100 時(shí)運(yùn)行。
在所有其他情況下,該函數(shù)不返回任何內(nèi)容并最終隱式返回
None
。
AttributeError: ‘NoneType’ object has no attribute ‘X’ 錯(cuò)誤解決方法
要解決這種情況下的錯(cuò)誤,我們要么必須檢查函數(shù)是否未返回 None
,要么在不滿足條件時(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)在,無論條件是否滿足,函數(shù)都保證返回一個(gè)值。
總結(jié)
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問 None
值的屬性時(shí),例如 來自不返回任何內(nèi)容的函數(shù)的賦值。 要解決該錯(cuò)誤,請?jiān)谠L問屬性之前更正分配。
到此這篇關(guān)于Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤的文章就介紹到這了,更多相關(guān)Python 中 AttributeError內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python錯(cuò)誤:AttributeError: ''module'' object has no attribute ''setdefaultencoding''問題的解決方法
- 解決python多線程報(bào)錯(cuò):AttributeError: Can''t pickle local object問題
- Python3下錯(cuò)誤AttributeError: ‘dict’ object has no attribute’iteritems‘的分析與解決
- 解決AttributeError:'NoneTypeobject'?has?no?attribute'Window'的問題(親測有效)
- Python進(jìn)程崩潰AttributeError異常問題解決
- 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à)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Pytorch中的自動(dòng)求梯度機(jī)制和Variable類實(shí)例
今天小編就為大家分享一篇Pytorch中的自動(dòng)求梯度機(jī)制和Variable類實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02關(guān)于PyTorch源碼解讀之torchvision.models
今天小編就為大家分享一篇關(guān)于PyTorch源碼解讀之torchvision.models,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08