自定义上下文管理器
class DatabaseConnection: def __enter__(self): print("连接数据库") return self def __exit__(self, exc_type, exc_val, exc_tb): print("关闭数据库连接") if exc_type: print(f"发生异常: {exc_val}") return False # 不抑制异常
contextlib 使用
from contextlib import contextmanager @contextmanager def timer(): start = time.time() try: yield finally: print(f"耗时: {time.time() - start:.2f}秒")
转载请注明:周志洋的博客 » Python进阶-上下文管理器