python with语句简介

在python中读写操作资源,最后需要释放资源。可以使用try…finally结构实现资源的正确释放,python提供了一个with语句能更简便的实现释放资源。

1. python像文件的操作open等已经可以直接使用with语句
2. 可以自定义一个支持with语句对象
3. 使用contextlib也可以使用with语句对象
4. 针对需要close操作的对象with的使用

示例代码中有4种使用标注
# 自定义支持with语句的对象
class DummyRes:

    def __init__(self, tag):
        self.tag = tag

    def __enter__(self):
        print("Enter >>> {}".format(self.tag))
        return self

    def __exit__(self, exc_type, exc_value, exc_tb):
        print("Exit <<< {}".format(self.tag))
        if exc_tb is None:
            print("Exit without Exception {}".format(self.tag))
            return False
        else:
            print("Exit with Exception {}".format(self.tag))
            return True

# 支持closing 上下文with语句对象
class Closing:

    def __init__(self, thing):
        self.thing = thing

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_tb):
        self.thing.close()

class ClosingDemo:

    def __init__(self):
        self.acquire()

    def acquire(self):
        print("Acquire RES")

    def close(self):
        print("Close RES")

from contextlib import contextmanager

class ContextDemo:

    def __init__(self):
        print("Context Demo init")
        raise Exception
        print("Context Demo init")

    def print(self):
        print("Context Demo print 1")
        #raise Exception
        print("Context Demo print 2")

    def close(self):
        print("Context Demo close")

def context_demo():
    print("context demo in")
    raise Exception
    print("context demo out")

@contextmanager
def demo():
    print("Allocate Resoures")
    try:
        yield context_demo
    finally:
        print("raise exception")
    #yield "*** contextmanager demo ***"
    print("Free Resoures")

if __name__ == "__main__":

    # 1. 使用with语句 (自动关闭文件)
    with open("test.txt", "w") as f:
        f.write("write test")

    # 2. 自动定义with语句
    with DummyRes("test") as res:
        print("With body 1")
        raise Exception
        print("With body 2")

    # 3. 利用contextlib定义with语句
    with demo():
        print("exc demo")

    # 4. closing 上下文 (适合有close操作的情况)
    with Closing(ClosingDemo()):
        print("Use Resoures")

你可能感兴趣的:(随笔,学习笔记)