import contextlib

@contextlib.contextmanager
def myopen(file, mode):
    f = open(file, mode, encoding="utf-8")
    try:
        yield f
    finally:
        f.close()
        
with myopen("01-thread.py", 'r') as f:
    print(f.read())

        这里使用Python contextlib模块模拟了我们常用的with open功能,这里使用了contextlib.contextmanager装饰器,不能缺失!