首页 > Python资料 博客日记
Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 错误
2024-02-29 15:00:05Python资料围观206次
文章目录
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 ‘X’ 常见原因
“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
。
AttributeError: ‘NoneType’ object has no attribute ‘X’ 错误解决方法
要解决这种情况下的错误,我们要么必须检查函数是否未返回 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 opencv)
- Python 图像处理进阶:特征提取与图像分类
- 大数据可视化分析-基于python的电影数据分析及可视化系统_9532dr50
- 【Python】入门(运算、输出、数据类型)
- 【Python】第一弹---解锁编程新世界:深入理解计算机基础与Python入门指南
- 华为OD机试E卷 --第k个排列 --24年OD统一考试(Java & JS & Python & C & C++)
- Python已安装包在import时报错未找到的解决方法
- 【Python】自动化神器PyAutoGUI —告别手动操作,一键模拟鼠标键盘,玩转微信及各种软件自动化
- Pycharm连接SQL Sever(详细教程)
- Python编程练习题及解析(49题)
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应
- Windows上安装 Python 环境并配置环境变量 (超详细教程)
- Python pyinstaller打包exe最完整教程