欧美农庄网站模板,网站开发公司合作协议书,建设部质监局网站,万网网站建设购买过程上下文管理器在使用Python编程中#xff0c;可以会经常碰到这种情况#xff1a;有一个特殊的语句块#xff0c;在执行这个语句块之前需要先执行一些准备动作#xff1b;当语句块执行完成后#xff0c;需要继续执行一些收尾动作。例如#xff1a;当需要操作文件或数据库的…上下文管理器在使用Python编程中可以会经常碰到这种情况有一个特殊的语句块在执行这个语句块之前需要先执行一些准备动作当语句块执行完成后需要继续执行一些收尾动作。例如当需要操作文件或数据库的时候首先需要获取文件句柄或者数据库连接对象当执行完相应的操作后需要执行释放文件句柄或者关闭数据库连接的动作。又如当多线程程序需要访问临界资源的时候线程首先需要获取互斥锁当执行完成并准备退出临界区的时候需要释放互斥锁。对于这些情况Python中提供了上下文管理器(Context Manager)的概念可以通过上下文管理器来定义/控制代码块执行前的准备动作以及执行后的收尾动作。上下文管理协议那么在Python中怎么实现一个上下文管理器呢这里又要提到两个”魔术方法”__enter__和__exit__下面就是关于这两个方法的具体介绍。__enter__(self) Defines what the context manager should do at the beginning of the block created by the with statement. Note that the return value of __enter__ is bound to the target of the with statement, or the name after the as.__exit__(self, exception_type, exception_value, traceback) Defines what the context manager should do after its block has been executed (or terminates). It can be used to handle exceptions, perform cleanup, or do something always done immediately after the action in the block. If the block executes successfully, exception_type, exception_value, and traceback will be None. Otherwise, you can choose to handle the exception or let the user handle it; if you want to handle it, make sure __exit__ returns True after all is said and done. If you don’t want the exception to be handled by the context manager, just let it happen.也就是说当我们需要创建一个上下文管理器类型的时候就需要实现__enter__和__exit__方法这对方法就称为上下文管理协议(Context Manager Protocol)定义了一种运行时上下文环境。with语句在Python中可以通过with语句来方便的使用上下文管理器with语句可以在代码块运行前进入一个运行时上下文(执行__enter__方法)并在代码块结束后退出该上下文(执行__exit__方法)。with语句的语法如下Pythonwith context_expr [as var]:with_suitecontext_expr是支持上下文管理协议的对象也就是上下文管理器对象负责维护上下文环境as var是一个可选部分通过变量方式保存上下文管理器对象with_suite就是需要放在上下文环境中执行的语句块在Python的内置类型中很多类型都是支持上下文管理协议的例如filethread.LockTypethreading.Lock等等。这里我们就以file类型为例看看with语句的使用。with语句简化文件操作当需要写一个文件的时候一般都会通过下面的方式。代码中使用了try-finally语句块即使出现异常也能保证关闭文件句柄。Pythonlogger open(log.txt, w) try:logger.write(Hello )logger.write(World) finally:logger.close()print logger.closed其实Python的内置file类型是支持上下文管理协议的可以直接通过内建函数dir()来查看file支持的方法和属性Python print dir(file)[__class__, __delattr__, __doc__, __enter__, __exit__, __format__, __getattribute__, __hash__, __init__, __iter__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclass hook__, close, closed, encoding, errors, fileno, flush, isatty, mode, name, newlines, next, read, readinto, readline, readlines, seek, softspace, tell, truncate, write, writelines, xreadlines] 所以可以通过with语句来简化上面的代码代码的效果是一样的但是使用with语句的代码更加的简洁Pythonwith open(log.txt, w) as logger:logger.write(Hello )logger.write(World)print logger.closed自定义上下文管理器对于自定义的类型可以通过实现__enter__和__exit__方法来实现上下文管理器。看下面的代码代码中定义了一个MyTimer类型这个上下文管理器可以实现代码块的计时功能Pythonimport timeclass MyTimer(object):def __init__(self, verbose False):self.verbose verbosedef __enter__(self):self.start time.time()return selfdef __exit__(self, *unused):self.end time.time()self.secs self.end - self.startself.msecs self.secs * 1000if self.verbose:print elapsed time: %f ms %self.msecs下面结合with语句使用这个上下文管理器Pythondef fib(n):if n in [1, 2]:return 1else:return fib(n-1) fib(n-2)with MyTimer(True):print fib(30)代码输出结果为异常处理和__exit__在使用上下文管理器中如果代码块 (with_suite)产生了异常__exit__方法将被调用而__exit__方法又会有不同的异常处理方式。当__exit__方法退出当前运行时上下文时会并返回一个布尔值该布尔值表明了”如果代码块 (with_suite)执行中产生了异常该异常是否须要被忽略”。1. __exit__返回False重新抛出(re-raised)异常到上层修改前面的例子在MyTimer类型中加入了一个参数”ignoreException”来表示上下文管理器是否会忽略代码块 (with_suite)中产生的异常。Pythonimport timeclass MyTimer(object):def __init__(self, verbose False, ignoreException False):self.verbose verboseself.ignoreException ignoreExceptiondef __enter__(self):self.start time.time()return selfdef __exit__(self, *unused):self.end time.time()self.secs self.end - self.startself.msecs self.secs * 1000if self.verbose:print elapsed time: %f ms %self.msecsreturn self.ignoreExceptiontry:with MyTimer(True, False):raise Exception(Ex4Test)except Exception, e:print Exception (%s) was caught %e else:print No Exception happened运行这段代码会得到以下结果由于__exit__方法返回False所以代码块 (with_suite)中的异常会被继续抛到上层代码。2. __exit__返回Ture代码块 (with_suite)中的异常被忽略将代码改为__exit__返回为True的情况Pythontry:with MyTimer(True, True):raise Exception(Ex4Test)except Exception, e:print Exception (%s) was caught %e else:print No Exception happened运行结果就变成下面的情况代码块 (with_suite)中的异常被忽略了代码继续运行一定要小心使用__exit__返回Ture的情况除非很清楚为什么这么做。3. 通过__exit__函数完整的签名获取更多异常信息对于__exit__函数它的完整签名如下也就是说通过这个函数可以获得更多异常相关的信息。__exit__(self, exception_type, exception_value, traceback)继续修改上面例子中的__exit__函数如下Pythondef __exit__(self, exception_type, exception_value, traceback):self.end time.time()self.secs self.end - self.startself.msecs self.secs * 1000if self.verbose:print elapsed time: %f ms %self.msecsprint exception_type: , exception_typeprint exception_value: , exception_valueprint traceback: , tracebackreturn self.ignoreException这次运行结果中就显示出了更多异常相关的信息了总结本文介绍了Python中的上下文管理器以及如何结合with语句来使用上下文管理器。总结一下with 语句的执行流程执行context_expr 以获取上下文管理器对象调用上下文管理器的 __enter__() 方法如果有 as var 从句则将 __enter__() 方法的返回值赋给 var执行代码块 with_suite调用上下文管理器的 __exit__() 方法如果 with_suite 产生异常那么该异常的 type、value 和 traceback 会作为参数传给 __exit__()否则传三个 None如果 with_suite 产生异常并且 __exit__() 的返回值等于 False那么这个异常将被重新抛出到上层如果 with_suite 产生异常兵器 __exit__() 的返回值等于 True那么这个异常就被忽略继续执行后面的代码在很多情况下with语句可以简化代码并增加代码的健壮性。本文来自投稿不代表访得立场如若转载请注明出处http://www.found5.com//view/393.html