zoukankan      html  css  js  c++  java
  • python的上下文管理(contextlib)(2)

    contextlib是一个Python模块,作用是提供更易用的上下文管理器。

    编写 __enter__ 和 __exit__ 仍然很繁琐,因此Python的标准库 contextlib 提供了更简单的写法,

    比如如下代码:

    from contextlib import contextmanager
     
    class Query(object):
     
        def __init__(self, name):
            self.name = name
     
        def query(self):
            print('Query info about %s...' % self.name)
     
    @contextmanager
    def create_query(name):
        print('Begin')
        q = Query(name)
        yield q
        print('End')

     @contextmanager 这个装饰器接受一个 generator,用 yield 语句把 with ... as var 把变量输出出去,然后,with 语句就可以正常的工作了:

    with create_query('Bob') as q:
        q.query()
     

    代码的执行顺序是:

    1.  with 语句 首先执行 yield 之前的语句,因此打印出 <h1>.
    2.  yield 调用会执行 with 语句内部的所有语句,因此打印出 hello 和 world.
    3.  最后执行yield之后的语句,打印出 </h1>.

     如果一个对象没有实现上下文,就不能使用 with 语句,但是可以用 closing() 来把对象变为上下文对象。

    1
    2
    3
    4
    5
    6
    from contextlib import closing
    from urllib.request import urlopen
     
    with closing(urlopen('https://www.python.org')) as page:
        for line in page:
            print(line)

      closing 也是一个经过 @contextmanager 装饰的generator

    1
    2
    3
    4
    5
    6
    @contextmanager
    def closing(thing):
        try:
            yield thing
        finally:
            thing.close()

      它的作用就是把任意对象变为上下文对象,并支持 with语句。

     

     

     @contextmanager 这个装饰器接受一个 generator,用 yield 语句把 with ... as var 把变量输出出去,然后,with 语句就可以正常的工作了:

    1
    2
    with create_query('Bob') as q:
        q.query()
  • 相关阅读:
    洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles
    洛谷 P2935 [USACO09JAN]最好的地方Best Spot
    CODEVS 1172 Hankson 的趣味题
    洛谷 P2261 [CQOI2007]余数求和
    洛谷 P1463 [POI2002][HAOI2007]反素数
    洛谷 P3383 【模板】线性筛素数
    1.4.2 solr字段类型--(1.4.2.1)字段类型定义和字段类型属性
    HttpSolrServer-采用静态工厂方法,创建HttpSolrServer单实例
    将字符转换为unicode码
    solrj-WiKi
  • 原文地址:https://www.cnblogs.com/aomi/p/7353939.html
Copyright © 2011-2022 走看看