Python with上下文管理及自定义上下文管理
发布日期:2021-05-12 20:11:31 浏览次数:11 分类:精选文章

本文共 2814 字,大约阅读时间需要 9 分钟。

Python ������������������������������������������

������������������

������������������������ Context Manager ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

with ������

with ��������� Python ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

with ������������������������

with open('test.txt', 'a') as f:    f.write('���������������\n')

���������������������������������������������test.txt������������������������������������������������������������������with ���������������������������������������������������f.close()���

��� with ���������������������������������������������������������������as ������������������������������������ f ������f ���������������/���������������������������with ������������������������������������������������������������������������������������������"������"���������

���������������������������

with ��������������������������������������� __enter__ ��� __exit__ ���������������������������__enter__ ���������������������������������������������������__exit__ ���������������������������������������������������������

���������������������������

������������ __enter__ ��� __exit__ ������������������������������������������������������

class OpenFile(object):    def __init__(self, file, mode):        self._file = file        self._mode = mode    def __enter__(self):        self._handle = open(self._file, self._mode)        return self._handle    def __exit__(self, exc_type, exc_val, exc_tb):        self._handle.close()with OpenFile('test01.txt', 'w') as f:    f.write('������������������������\n')

��������������������������� OpenFile ������������������������������������������������������������������������������������������

���������������������������������

__exit__ ������������������������ exc_type���exc_val ��� exc_tb������������������������ with ��������������������������� __exit__ ������ True������������������������������������������������������������

class OpenFile(object):    def __init__(self, file, mode):        self._file = file        self._mode = mode    def __enter__(self):        self._handle = open(self._file, self._mode)        return self._handle    def __exit__(self, exc_type, exc_val, exc_tb):        self._handle.close()        if exc_type == "None":            print('������������')        else:            print(f'Exception: {exc_type}')        return Truewith OpenFile('test01.txt', 'r') as f:    f.write('������������������������\n')

������������ __exit__ ������ True������������������������������������������ exc_type ������������������������������

上一篇:Python列表排序 list.sort方法和内置函数sorted
下一篇:Python异常捕获及自定义异常类

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月16日 00时41分29秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

GZIP压缩和解压缩不删除原始文件 2021-05-13
【无线通信模块】GPRS DTU不稳定和容易掉线原因 2021-05-13
CSS(六)|页面布局之定位 2021-05-13
比特币(BSV)知识库:身份-BSVAlias 2021-05-13
比特币(BSV)知识库:网络-比特币测试用区块链(Bitcoin Test Blockchains) 2021-05-13
设计模式 - 2) 策略模式 2021-05-13
SpringBoot使用RedisTemplate简单操作Redis的五种数据类型 2021-05-13
国标流媒体服务器以ROOT身份运行提示“permission denide”报错解决 2021-05-13
国标流媒体服务器在linux系统运行提示fork/exec ……/redis/redis-server错误解决方案 2021-05-13
国标GB28181协议视频推流平台EasyGBD在Linux下编译报“UINT64_C在此作用领域中尚未声明”错误 2021-05-13
视频流媒体服务器RTSP拉流、RTMP推流流媒体服务器授权方案之加密机运行后无法授权问题解决 2021-05-13
安防摄像机网页无插件直播方案EasyNVR关于接口调用出现401 Unauthorized问题的解决方法 2021-05-13
EasyNVR视频流媒体服务器出现启动错误的解决方案(二) 2021-05-13
如何在农业或大棚内布置互联网安防监控系统实现智慧农业? 2021-05-13
【视频教程】EasyNVR如何将老版本的EasyNVR的数据迁移到4.0.0以上版本 2021-05-13
LeetCode 872 叶子相似的树[DFS 二叉树] HERODING的LeetCode之路 2021-05-13
qt中转到槽后如何取消信号与槽关联 2021-05-13
qt问题记录-spin box与double spin box 2021-05-13
python向Excel读取一行数据 2021-05-13
OpenGL学习教程 2021-05-13