Python 的with关键字

看别人的代码时,with关键字经常会出现,博主决定梳理一下with以及python中上下文(context)的概念

1. 上下文管理器概念

Context Manager指的是python在执行一段代码前后,做的一些预处理和后处理,使得代码块运行处于一个小的环境(surrounding),出了这个小环境之后,资源释放,环境中的各种配置也失效。

例如在打开文件需要关闭,连接数据库后需要关闭连接。很多优雅第三方库也会利用上下文使得对象进入特定的某种状态。

2. with关键字

with的基本用法如下:

with EXPR as VAR:
BLOCK

其中发生了一系列过程:

  1. EXPR语句被执行,得到ContextManager
  2. 调用ContextManager.__enter__方法
  3. 如果有as VAR,则ContextManager.__enter__的返回值赋给VAR,否则就不管返回值
  4. 执行BLOCK,如果有VAR被用到,就和普通变量一样
  5. 调用ContextManager.__exit__方法
    • __exit__有三个参数:type, value, traceback,BLOCK出异常时会得到对应值,正常情况就都为None
    • __exit__返回值为True表示BLOCK中出现的异常可以忽略,False表示需要raise

3. 例子

3.1 资源操作:

class CustomOpen:
def __init__(self, filename: str):
self.__filename = filename
self.__handler = None def __enter__(self):
print("enter......")
self.__handler = open(self.__filename)
return self.__handler def __exit__(self, exc_type, exc_val, exc_tb):
print("exit...", exc_type, exc_val, exc_tb)
if self.__handler is not None:
self.__handler.close()
return True with CustomOpen("hello.txt") as f:
print(f.read())

运行结果:

enter......
hello world
exit... None None None

3.2 状态维护

class CustomBrain:
def __init__(self):
self.__status = "normal" def say(self):
if self.__status == "normal":
print("You're a great man")
elif self.__status == "special":
print("You are a very outstanding person ") def __enter__(self):
self.__status = "special" def __exit__(self, exc_type, exc_val, exc_tb):
self.__status = "normal" brain = CustomBrain()
brain.say() # 普通状态 # 可以通过上下文维护一些状态
with brain:
brain.say() # 特殊状态 brain.say() # 普通状态

运行结果:

You're a great man
You are a very outstanding person
You're a great man

4. 使用contextlib简化编写

python内置的标准库contextlib可以是的代码书写更加简洁,本质是一样的。比较有用的是contextlib.contextmanager这个装饰器,被装饰的函数在yield的前面相当于__enter__,yield的后面相当于__exit__,yield本身的返回值赋给as后的变量

所以第一个示例可以这么写:

from contextlib import contextmanager

@contextmanager
def custom_open(filename: str):
print("enter......")
handler = open(filename) yield handler print("exit...")
handler.close() with custom_open("hello.txt") as f:
print(f.read())

还是优雅了许多~

Python 的with关键字的更多相关文章

  1. Python中的关键字的用法

    Python有哪些关键字 -Python常用的关键字 and, del, from, not, while, as, elif, global, or, with, assert, else, if, ...

  2. python的标识符&&关键字

    和Java语言一样,python也有标识符和关键字.那么,你是否知道python的关键字呢?一起先从标识符了解python吧. 什么是标识符? 标识符,开发人员在开发过程中自定义的一些符号和名称. 标 ...

  3. python中super关键字的用法

    http://python.jobbole.com/86787/ class A: def __init__(self):    print "enter A"    print ...

  4. python正则提取关键字

    python使用正则表达式提取关键字 import sys reload(sys) sys.setdefaultencoding("utf-8") import re ss = & ...

  5. python 位置参数和关键字参数 *args **kwargs

    #!/usr/bin/env pythondef foo(*args,**kwargs): print('args: {0}'.format(args)) print('kwargs {0}'.for ...

  6. python进阶之关键字和运算符触发魔法方法

    前言 python有众多的魔法方法,它们会在满足某种条件下触发执行,掌握好魔法方法的使用,可以加快程序的运行效率,同时减少逻辑调用. 关键字与魔法方法 python的一些魔法方法是关键字触发的,即py ...

  7. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

  8. python语言(四)关键字参数、内置函数、导入第三方模块、OS模块、时间模块

    一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会 ...

  9. 怎样查看python的所有关键字

    关键字是python中具有特定功能的一组词汇, 这些词汇不能用作变量名, 一般会有高亮提示, code时请小心. python的关键字其实也是python的语法核心, 掌握了所有python关键字的用 ...

随机推荐

  1. java开发环境构建

    一. 基本工具安装 1. 配置终端命令别名 vim ~/.bash_profile *********************************************** # for colo ...

  2. 7、TortoiseSVN

    7.TortoiseSVN TortoiseSVN图标介绍: 目录空白处右键→TortoiseSVN→Settings 7.1独立将工程上传到服务器的思路 12.2针对archetype-catalo ...

  3. Perl 认识简介

    Perl简介 Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl 是高级.通用.直译 ...

  4. Windows环境中编译opencv3.0同时加入OPENCV_contrib库及解决遇到相关问题[contrib 必须要3.1以上的opencv才支持了]

    更新:现在contrib库必须要opencv3.1以上才能支持编译通过了. 方法和步骤还是和本篇文章一样. ############################################## ...

  5. S19格式

    S-record格式文件是Freescale CodeWarrior编译器生成的后缀名为.S19的程序文件,是一段直接烧写进MCU的ASCII码,英文全称问Motorola format for EE ...

  6. ios h5 长按时出现黑色透明遮罩

    html,body{-webkit-text-size-adjust: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);}

  7. 网卡绑定(bonding)

    就是将多块网卡绑定同一IP地址对外提供服务,可以实现高 可用或者负载均衡.当然,直接给两块网卡设置同一IP地址 是不可能的.通过bonding,虚拟一块网卡对外提供连接, 物理网卡的被修改为相同的MA ...

  8. [angular2/4/8]用ng new创建项目卡住的解决办法

    官方文档 英文版:https://angular.io/guide/quickstart 中文版:https://angular.cn/guide/quickstart Installing pack ...

  9. ES head

    第2种安装方式 第二种方式就是不通过Elasticsearch插件方式进行安装 1.下载elasticsearch-head的源码包 地址:https://github.com/mobz/elasti ...

  10. sysbench简易使用

    sysbench简易使用 由于测试需要,需要用到sysbench这个工具.推荐简便使用. # yum 安装 yum install sysbench 创建数据库 CREATE DATABASE `sb ...