while this might look like magic, the way Python handles with is more clever than magic. The basic idea is that the statement after with has to evaluate an object that responds to an __enter__() as well as an __exit__() function.
After the statement that follows with is evaluated, the __enter__() function on the resulting object is called. The value returned by this function is assigned to the variable following as. After every statement in the block is evaluated, the __exit__() function is called.
例子如下:
class RedirectionStdoutTo:
def __init__(self, fd):
self.out_new = fd
def __enter__(self):
self.old_out = sys.stdout
sys.stdout = self.out_new
def __exit__(self):
sys.stdout = self.old_out
with open('Logfile') as fd:
with RedirectionStdoutTo(fd):
do_something()
这里open函数返回的file object对象实现了__enter__和__exit__方法,RedirectionStdoutTo()实例也实现了__enter__和__exit__方法;第一个with返回了file object对象供后续使用,第二个未明确在as后引用
这里自行写了一个小类实现tcp连接的关闭:
import socket
class mysocket:
def __init__(self, sockfd):
self.sockfd = sockfd
def __enter__(self):
print "Enter"
def __exit__(self, para1, para2, para3):
print "Exit"
self.sockfd.close()
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockfd.connect(("www.taobao.com", 80))
with mysocket(sockfd):
pass