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已将为我们做好了这个操作,这就可以理解了。

相关推荐
IVEN_4 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang6 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮6 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling6 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮9 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽9 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers