上下文管理器是一个对象,它定义了在执行 with 语句时要建立的运行时上下文。 上下文管理器处理进入和退出所需运行时上下文以执行代码块。
通常使用 with 语句(在 with 语句中描述),但是也可以通过直接调用它们的方法来使用。
上下文管理器是一个实现了__enter__、__exit__魔法方法的类对象
定义了__enter__、__exit__方法的类对象即可用作上下文管理器
class A: def __init__(self): print("__init__") def __enter__(self): print("__enter__") def __exit__(self, exc_type, exc_val, exc_tb): print("__exit__") if __name__ == "__main__": with A() as f: print("running ...")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Door(): def __enter__( self ): print ( 'Begin' ) return self def __exit__( self , exc_type, exc_value, traceback): if exc_type: print ( 'Error' ) else : print ( 'End' ) def open ( self ): print ( 'Door is opened' ) def close( self ): print ( 'Door is closed' ) if __name__ = = "__main__" : with Door() as d: d. open () d.close() |
exit()方法中有3个参数, exc_type, exc_val, exc_tb,这些参数在异常处理中相当有用。
exc_type: 错误的类型
exc_val: 错误类型对应的值
exc_tb: 代码中错误发生的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Sample(): def __enter__( self ): print ( 'in enter' ) return self def __exit__( self , exc_type, exc_val, exc_tb): print "type: " , exc_type print "val: " , exc_val print "tb: " , exc_tb def do_something( self ): bar = 1 / 0 return bar + 10 with Sample() as sample: sample.do_something() in enter Traceback (most recent call last): type : < type 'exceptions.ZeroDivisionError' > val: integer division or modulo by zero File "/home/user/cltdevelop/Code/TF_Practice_2017_06_06/with_test.py" , line 36 , in <module> tb: <traceback object at 0x7f9e13fc6050 > sample.do_something() File "/home/user/cltdevelop/Code/TF_Practice_2017_06_06/with_test.py" , line 32 , in do_something bar = 1 / 0 ZeroDivisionError: integer division or modulo by zero Process finished with exit code 1 |
在with后面的代码块抛出异常时,exit()方法被执行。开发库时,清理资源,关闭文件等操作,都可以放在exit()方法中。
总之,with-as表达式极大的简化了每次写finally的工作,这对代码的优雅性是有极大帮助的。
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统