6-contextlib

作者: 神秘加冰 | 来源:发表于2019-01-15 17:25 被阅读0次

    contextlib

    在python 中只要正确实现了上下文管理管理,就可以使用 with 语句。而 contextlib 就是一个帮助实现上下文管理的模块。
    所以什么是上下文管理? 一般来说,只要实现了 enter() 和 exit() 两个方法,在python 中就算是实现了上下文管理
    一个简单的例子

    class Context():
        def __init__(self):
            print("__init__")
        
        def __enter__(self):
            print("__enter__")
        
        def __exit__(self,exc_type, exc_val, exc_tb):
            print("__exit__")
    
    with Context():
        print("doing some works")
    

    contextmanager装饰器

    contextmanager在使用中应该算是最常见的。contextmanager是一个装饰器,用于实现上下文管理。 但首先,要明确的一点是能够使用 with 语句不一定就是说能够正确的关闭资源,能够使用with 只是说明了实现了上下文管理器。比如:

    @contextmanager
    def tag():
        print("<h1>")
        yield 
        print("</h1>")
    
    
    with tag():
        print("Hello world")
    

    这里也可以使用 with 但是只是实现了tag 函数的上下文管理,没有关闭任何资源。其输出:

    <h1>
    Hello world
    </h1>
    

    如果跟关闭资源有关,我们需要在函数的上下文中定义好关闭资源的方法,比如以连接mysql数据库为例:

    from contextlib import contextmanager
    import pymysql
    
    # 定义连接上下文
    @contextmanager
    def create_connection():
        connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='qwe123',
                                 db='apitesting',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
        try:
            yield connection
        finally:
            connection.close()
    
    # 使用 with...as 输出上下文管理
    with create_connection() as conn:
        with conn.cursor() as cursor:
            sql = "select * from account;"
            cursor.execute(sql)
            result = cursor.fetchone()
            print(result)
        conn.commit()
    

    所以关闭资源是关闭资源,跟上下文管理不存在关系。

    closing

    如果一个对象没有实现上下文,就不能使用with语句. 这个时候可以使用closing 将其装饰为一个实现了上下文管理的对象,但是注意的是,对象必须要有 close 方法。

    from contextlib import closing
    
    class TestObj():
        def __init__(self):
            print("init test obj")
        def test(slef):
            print("test")
        def close(self):
            print("close")
        
    
    with closing(TestObj()) as t:
        t.test()
    

    相关文章

      网友评论

        本文标题:6-contextlib

        本文链接:https://www.haomeiwen.com/subject/btgydqtx.html