with上下文管理器

http://blog.csdn.net/Shiroh_ms08/article/details/53859475

# 玩with语句
class TestWith(object):
    def __init__(self):
        print("this is init of TestWith")

    def if_exist(self):
        print("I'm here")

    def __enter__(self):
        print("This is TestWith __enter__")
        return self

    def __exit__(self, *args):
        for i in args:
            print(i)
        print("Now __exit__")


with TestWith() as t:
    t.if_exist()

  

posted @ 2018-02-26 17:46  安迪9468  阅读(112)  评论(0编辑  收藏  举报