python with 管理器 上文下文管理器 class 详解
#!/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):pr
·
#!/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 self
def __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执行了
'''
更多推荐



所有评论(0)