[00803]让对象支持上下文管理协议

时间:2023-01-20 16:54:28

让对象支持上下文管理协议

一、 解决问题

想让对象支持上下文管理协议(with语句)。

二、解决方案

重新–enter–(), –exit–()
让对象能够兼容with语句,比如:获取资源和释放资源的类

三、代码说明

#!/usr/bin/env python

from socket import socket, AF_INET, SOCK_STREAM

class LazyConnection:
def __init__(self, address, family=AF_INET, type=SOCK_STREAM):
self.address = address
self.family = family
self.type = type
self.sock = None

def __enter__(self):
if self.sock is not None:
raise RuntimeError('Alrady connected')
self.sock = socket(self.family, self.type)
self.sock.connect(self.address)
return self.sock

# exc_ty:异常类型,exc_val:异常值,tb:对挂起异常的追溯
# 如果忽略异常的话,返回None作为结果。
# 如果返回True(),异常就被会任务清理干净了。
def __exit__(self, exc_ty, exc_val, tb):
self.sock.close()
self.sock = None
from functools import partial

conn = LazyConnection(('www.python.org', 80))

with conn as s:
# conn.__enter__() executes: connection open
s.send(b'GET /index.html HTTP/1.0\r\n')
s.send(b'Host: www.python.org\r\n')
s.send(b'\r\n')
resp = b''.join(iter(partial(s.recv, 8192), b''))
print (resp)
# conn.__exit() execuste:connection closed

四、关联知识

contextmange 模块 传送门,待补充

五、总结

六、代码地址

github地址:https://github.com/weichen666/python_cookbooka>
目录/文件:eight_selection/learn_class_obj_with.py

七、参考