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