当前位置: 代码迷 >> 综合 >> python with 管理器 上文下文管理器 class 详解
  详细解决方案

python with 管理器 上文下文管理器 class 详解

热度:65   发布时间:2023-10-10 22:12:54.0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/13 21:01
# @Author  : flyx
# @Site    : 
# @File    : withdemo2.py
# @Software: PyCharm
'''这个类 遵守了上下文管理器协议
'''
class mycontent(object):def __enter__(self):print('enter执行l ')return selfdef __exit__(self, exc_type, exc_val, exc_tb):print('exit执行了')def show(self):print('show 执行')with mycontent() as my:my.show()'''
实例对象就是上下文管理器
enter执行l 
show 执行
exit执行了
'''

 

  相关解决方案