学习笔记(18):21天通关Python(仅视频课)-管道输入与with语句

Ophelia ·
更新时间:2024-09-20
· 624 次阅读

立即学习:https://edu.csdn.net/course/play/24797/282198?utm_source=blogtoedu

# import sys # for i in sys.stdin: # print(i) class TestWith: def __init__(self, tag): self.tag = tag print('构造函数——init——') def __enter__(self): print('运行__enter__方法tag=%s' % self.tag) return '返回enter方法结果' ''' 异常信息 exc_type:异常类型 exc_val:异常传入的value exc_tb:异常的traceback ''' def __exit__(self, exc_type, exc_val, exc_tb): if exc_tb: print('此处有异常') else: print('程序运行正常') with TestWith('testWith') as re: print('re:%s' % re) print('开始') # raise Exception(2, '错误') print('结束') with open('test45.py', 'r', True, 'utf-8') as f: for i in f: print(i, end='')
作者:Hello-Rock



with 学习笔记 输入 学习 管道 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号