当前位置:网站首页>Magic method "six"__ enter__ And__ exit__

Magic method "six"__ enter__ And__ exit__

2022-06-22 03:28:00 Fenghua Langlang

https://www.cnblogs.com/DragonFire/p/6764066.html

Context manager

class Sample():

    def __enter__(self):
        #  Access to resources 
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        #  Release resources 
        print('exit')

    def do_something(self):
        print('doing something')

with Sample() as sample:
    sample.do_something()
原网站

版权声明
本文为[Fenghua Langlang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220312376332.html