Python with/as Context Managers
This statement is designed to work with context manager objects, which support a new method-based protocol, similar in spirit to the way that iteration tools work with methods of the iteration protocol.
Basic Usage
The basic format of the with statement looks like this, with an optional part in square brackets here:
with express [as varial]: with - block |
The expression here is assumed to return an object that supports the context management porotocol. This object may also return value that will be assigned to the name variable if the optional as clause is present.
Note that the variable is not necessarily assigned the result of the expression. the result of the expression is the object that supports the context protocol, and the variable may be assigned something else intended to be used inside the statement. The object returned by the expression may then run startup code before the with-block is started, as well as termination code after the block is done, regardless of whether the block raise an exception or not.
A built-in Python object has been augmented to support the context management protocol, and so can be used with the with statement.
>>> with open (r "G:\test.txt" ) as file : for line in file : print (line) |
The context Management Protocol
To implement context management, classes use special methods that fall into the operator overloading category to tap into the with statement. This interface expected of objects used in with statement is somewhat complex, and most programmers only need to know how to use existing context manager.
Here’s how the with statement works:
- The expression is valuated, resulting in an object known as a context manager that must have __enter__ and __exit__ method.
- The context manager’s __enter__ method is called. The value it return is assigned to the variable in the as clause if present, or simply discarded otherwise.
- The code in the nested with block is executed.
- If the block raises an exception, the __exit__(type, value, traceback) method is called with the exception in the Python manuals and later in this part of the book. If the method returns a false value, the exception is raised. Otherwise, the exception is terminated. The exception should normally be reraised so that it is propagated outside the with statement.
- If the with block does not raise an exception, the __exit__ method is still called, but its type, value and traceback arguments are passed in as None.
An example of context management in action:
>>> class TraceBlock( object ): def __enter__( self ): print ( "call __enter__ function" ) return self def __exit__( self , exception_type, exception_value, exception_trace): print ( "call __exit__ function" ) print ( "exception info: type:%s value:%s" % (exception_type, exception_value)) def action( self , value): print ( "call action method" ) print (value) >>> with TraceBlock() as I: print ( "start" ) I.action( "exit normally" ) print ( "end" ) call __enter__ function start call action method exit normally end call __exit__ function exception info: type : None value: None >>> >>> with TraceBlock() as I: print ( "start" ) I.action( "raise exception" ) pritn( "end" ) #not call the block call __enter__ function start call action method raise exception call __exit__ function exception info: type :< type 'exceptions.NameError' > value:name 'pritn' is not defined Traceback (most recent call last): File "<pyshell#684>" , line 4 , in <module> pritn( "end" ) #not call the block NameError: name 'pritn' is not defined >>> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 如何打造一个高并发系统?
· 《SpringBoot》EasyExcel实现百万数据的导入导出