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:

  1. The expression is valuated, resulting in an object known as a context manager that must have __enter__ and __exit__ method.
  2. 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.
  3. The code in the nested with block is executed.
  4. 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.
  5. 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
>>>
posted @   hotbaby  阅读(259)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
阅读排行:
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 如何打造一个高并发系统?
· 《SpringBoot》EasyExcel实现百万数据的导入导出
点击右上角即可分享
微信分享提示