关于 with 语句

class C(object):
    def __enter__(self):
        print('jinru')
        return self


    def __exit__(self, exc_type, exc_val, exc_tb):
        print('chuqu')


with C() as f:
    print(f)
    print(123)

with语句  在内部有一个 __enter__  和  __exit__  的双下方法 先执行 __enter__ 在做操作 再执行__exit__   退出

原文地址:https://www.cnblogs.com/a438842265/p/12118439.html