#!/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执行了
'''

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐