在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作;当语句块执行完成后,需要继续执行一些收尾动作。例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资源的加锁和解锁等情况。
对于这种情况python提供了上下文管理器(Context Manager)的概念,可以通过上下文管理器来定义/控制代码块执行前的准备动作,以及执行后的收尾动作。

一、为何使用上下文管理器

1、不使用上下文管理器的情况
通过try...finally语句执行异常处理和关闭句柄的动作。

logger = open("log.txt", "w")
try:
  logger.write('Hello ')
  logger.write('World')
finally:
  logger.close() print logger.closed

2、使用上下文管理器
默认文件Python的内置file类型是支持上下文管理协议的。
使用上下文管理器with使得依据精简了很多。

with open("log.txt", "w") as logger:
  logger.write('Hello ')
  logger.write('World') print logger.closed

二、实现上下文管理器实现上下文管理器有两种方式实现。方法一:类实现__enter__和__exit__方法。方法二:contextlib模块装饰器和生成器实现。
下面我们通过两种方法分别实现一个自定义的上下文管理器。
1、方法一:通过类实现__enter__和__exit__方法

class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
self.file_obj.close() with File('demo.txt', 'w') as opened_file:
opened_file.write('Hola!')

实现__enter__和__exit__方法后,就能通过with语句进行上下文管理。

a、底层都发生了什么?

1、with语句先暂存了File类的__exit__方法,然后它调用File类的__enter__方法。
2、__enter__方法打开文件并返回给with语句,打开的文件句柄被传递给opened_file参数。
3、with语句调用之前暂存的__exit__方法,__exit__方法关闭了文件。

b、异常处理

关于异常处理,with语句会采取哪些步骤。
1. 它把异常的type,value和traceback传递给__exit__方法
2. 它让__exit__方法来处理异常
3. 如果__exit__返回的是True,那么这个异常就被忽略。
4. 如果__exit__返回的是True以外的任何东西,那么这个异常将被with语句抛出。

异常抛出

#异常抛出,_exit__返回的是True以外的任何东西,那么这个异常将被with语句抛出
class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
self.file_obj.close()
print "type:",type
print "value:",value
print "traceback:",traceback with File('demo.txt', 'w') as opened_file:
opened_file.undefined_function('Hola!') #output================================================ # type: <type 'exceptions.AttributeError'>
# value: 'file' object has no attribute 'undefined_function'
# traceback: <traceback object at 0x000000000262D9C8> # opened_file.undefined_function('Hola!')
# AttributeError: 'file' object has no attribute 'undefined_function'

异常忽略:

#异常忽略,__exit__返回的是True,那么这个异常就被忽略。
class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, exception_type, exception_value, traceback):
print("Exception has been handled")
self.file_obj.close()
return True with File('demo.txt', 'w') as opened_file:
opened_file.undefined_function('Hola!') # output==================================
# Exception has been handled

2、方法二:contextlib模块装饰器和生成器实现

这种方式实现更优雅,我个人更喜欢这种方式。

yield之前的代码由__enter__方法执行,yield之后的代码由__exit__方法执行。本质上还是__enter__和__exit__方法。

# coding:utf-8
import contextlib @contextlib.contextmanager
def myopen(filename, mode):
f = open(filename, mode)
try:
yield f.readlines()
except Exception as e:
print e finally:
f.close() if __name__ == '__main__':
with myopen(r'c:\ip2.txt', 'r') as f:
for line in f:
print line

3、with语句上多个下文关联

直接通过一个with语句打开多个上下文,即可同时使用多个上下文变量,而不必需嵌套使用with语句。

class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, exception_type, exception_value, traceback):
self.file_obj.close()
return True with File('demo.txt', 'w') as f1,File('demo.txt','w') as f2:
print f1,f2 # Output============================# <open file 'demo.txt', mode 'w' at 0x000000000263D150> <open file 'demo.txt', mode 'w' at 0x000000000263D1E0>

python with语句上下文管理的两种实现方法的更多相关文章

  1. python下timer定时器常用的两种实现方法

    方法一,使用线程中现成的:   这种一般比较常用,特别是在线程中的使用方法,下面是一个例子能够很清楚的说明它的具体使用方法: #! /usr/bin/python3 #! -*- conding: u ...

  2. Python中的上下文管理器和with语句

    Python2.5之后引入了上下文管理器(context manager),算是Python的黑魔法之一,它用于规定某个对象的使用范围.本文是针对于该功能的思考总结. 为什么需要上下文管理器? 首先, ...

  3. 【Python】【上下文管理器】

    """#[备注]#1⃣️try :仅当try块中没有异常抛出时才运行else块.#2⃣️for:仅当for循环运行完毕(即for循环没有被break语句终止)才运行els ...

  4. Python - Context Manager 上下文管理器

    什么是上下文管理器 官方解释... 上下文管理器是一个对象 它定义了在执行 with 语句时要建立的运行时上下文 上下文管理器处理进入和退出所需的运行时上下文以执行代码块 上下文管理器通常使用 wit ...

  5. (转)Python中的上下文管理器和Tornado对其的巧妙应用

    原文:https://www.binss.me/blog/the-context-manager-of-python-and-the-applications-in-tornado/ 上下文是什么? ...

  6. python 读取wav 音频文件的两种方式

    python 中,常用的有两种可以读取wav音频格式的方法,如下所示: import scipy from scipy.io import wavfile import soundfile as sf ...

  7. Python之面向对象上下文管理协议

    Python之面向对象上下文管理协议 析构函数: import time class Open: def __init__(self,filepath,mode='r',encode='utf-8') ...

  8. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

  9. python 两种排序方法 sort() sorted()

    python中有两种排序方法,list内置sort()方法或者python内置的全局sorted()方法 区别为: sort()方法对list排序会修改list本身,不会返回新list.sort()只 ...

随机推荐

  1. Android读取RAM,ROM,SD卡容量

    1)简介 一般人们在买手机的时候,在手机配置上都会出现"内存容量:512MB ROM+512MB RAM "等等类似这样的说明,可能很多人都知道RAM的意思就是运存的意思,但是对于 ...

  2. 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005

    https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...

  3. POJ 3422 Kaka's Matrix Travels K取方格数

    题目:给出n*n的方格矩阵,现在从左上方走m次到右下方,问m次能够获得的最大价值和. 分析:最大费用流.拆点进行限制每个格子只取一次,假设点x拆成 x,xx,右边(假设有)y,yy,下方(假设有)z, ...

  4. 分页控件(PageControl)的相关属性说明

    UIPageControl *pageControl = [[UIPageControl alloc] init]; pageControl.center = CGPointMake(w * ); p ...

  5. 设置背景为白色,避免从A视图跳转到B视图的时候出现卡顿

    - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; }

  6. PBS常用指令合集

    以下以任务名 job.pbs对应任务ID 12341234为代表,提交者用户名为user. 1.基本指令-最常用 提交作业 qsub job.pbs 查询全部作业 qstat 查询个人作业 qstat ...

  7. 【CSS3】---练习制作导航菜单

    练习题 根据所学知识,使用CSS3实现下图的导航菜单效果 任务 1.制作导航圆角 提示:使用border-radius实现圆角 2.制作导航立体风格 提示:使用box-shadow实现立体风格 3.制 ...

  8. 出现,视图必须派生自 WebViewPage 或 WebViewPage错误解决方法

    遇到这种问题是因为我新建了Areas,在Areas里面建Controllers,Models,Views.所以在View文件夹下面湿没有Web.config文件的. 解决方法:(复制views中的we ...

  9. php数组中删除元素之重新索引

    如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 <?php $arr = array('a','b','c','d'); unset($arr[1]); ...

  10. in/exists not in/not exists null

    in/not in exists/not exists null的理解 两个测试表 create table tmp01 as with tmp as ( select '1' as id from ...