python中with用法讲解

我们都知道打开文件有两种方法:

f = open() with open() as f:

这两种方法的区别就是第一种方法需要我们自己关闭文件;f.close(),而第二种方法不需要我们自己关闭文件,无论是否出现异常,with都会自动帮助我们关闭文件,这是为什么呢?

我们先自定义一个类,用with来打开它:

?

|----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Foo(): ``def __enter__(``self``): ``print``(``"enter called"``) ``def __exit__(``self``, exc_type, exc_val, exc_tb): ``print``(``"exit called"``) ``print``(``"exc_type :%s"``%``exc_type) ``print``(``"exc_val :%s"``%``exc_val) ``print``(``"exc_tb :%s"``%``exc_tb) with Foo() as foo: ``print``(``"hello python"``) ``a ``= 1``/``0 ``print``(``"hello end"``) |

执行结果:

?

|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 | enter called Traceback (most recent call last): hello python exit called exc_type :<``class 'ZeroDivisionError'``> exc_val :division by zero ``File "F:/workspaces/python_workspaces/flask_study/with.py"``, line ``25``, ``in <module> ``a ``= 1``/``0 exc_tb :<traceback ``object at ``0x0000023C4EDBB9C8``> ZeroDivisionError: division by zero Process finished with exit code ``1 |

我们看到,执行结果的输入顺序,分析如下:

当我们with Foo() as foo:时,此时会执行__enter__方法,然后进入执行体,也就是:

?

|-------|----------------------------------------------------------------------------|
| 1 2 3 | print``(``"hello python"``) a ``= 1``/``0 print``(``"hello end"``) |

语句,但是在a=1/0出现了异常,with将会中止,此时就执行__exit__方法,就算不出现异常,当执行体被执行完毕之后,__exit__方法仍然被执行一次。

我们回到with open("file")as f: 不用关闭文件的原因就是在__exit__方法中,存在关闭文件的操作,所以不用我们手工关闭文件,with已将为我们做好了这个操作,这就可以理解了。

相关推荐
小小测试开发6 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想7 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波7 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)8 小时前
【无标题】
开发语言·c#
copyer_xyf8 小时前
Python 异常处理
前端·后端·python
XBodhi.8 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
麻雀飞吧8 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy8 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.9 小时前
【01】Python 机器学习
开发语言·python
为爱停留9 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python