Python 中 AttributeError: 'NoneType' object has no attribute 'X' 错误
Python “AttributeError: 'NoneType' object has no attribute” 发生在我们尝试访问 None 值的属性时,例如 来自不返回任何内容的函数的赋值。 要解决该错误,请在访问属性之前更正分配。
这是一个非常简单的示例,说明错误是如何发生的。
example = None
# ⛔️ AttributeError: 'NoneType' object has no attribute 'append'
example.append('hello')
尝试访问或设置 None 值的属性会导致错误。
如果打印正在访问其属性的变量,它将为 None,因此您必须追踪变量被分配 None 值的位置并更正分配。
None 值(显式赋值除外)的最常见来源是不返回任何内容的函数。
# 👇️ 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
函数没有显式返回值,因此它隐式返回 None。
我们将调用函数的结果分配给一个变量,并试图访问导致错误的属性。
“AttributeError: 'NoneType' object has no attribute” 的发生有多种原因:
-
具有不返回任何内容的函数(隐式返回
None
)。 -
将变量显式设置为
None
。 - 将变量分配给调用不返回任何内容的内置函数的结果。
- 具有仅在满足特定条件时才返回值的函数。
一些内置方法(例如排序)会就地改变数据结构并且不返回值。 换句话说,它们隐式返回 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()
方法对列表进行就地排序并且不返回任何内容,因此当我们将调用该方法的结果分配给变量时,我们为该变量分配了一个 None 值。
避免使用会改变原始对象的方法进行赋值,因为它们中的大多数不返回值并隐式返回
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
块。
错误的另一个常见原因是具有仅在满足条件时才返回值的函数。
def get_num(a):
if a > 100:
return a
result = get_num(5)
print(result) # 👉️ None
get_num
函数中的 if
语句仅在传入的参数大于 100 时运行。
在所有其他情况下,该函数不返回任何内容并最终隐式返回
None
。
要解决这种情况下的错误,我们要么必须检查函数是否未返回 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
现在,无论条件是否满足,函数都保证返回一个值。
总结
Python “AttributeError: 'NoneType' object has no attribute” 发生在我们尝试访问 None
值的属性时,例如 来自不返回任何内容的函数的赋值。 要解决该错误,请在访问属性之前更正分配。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。