套接字 __exit__ 在 python 中关闭了吗?
does socket __exit__ close in python?
我想知道是否:
with open_the_socket() as s:
use s
按预期工作。我在另一个问题上读到,只要套接字的 exit 函数调用关闭,它就可以工作。据说 2.7 没有,但我正在使用 3.4,我只是想知道。
这是来自 Python 3.4.0 的 socket.py 的片段:
def __exit__(self, *args):
if not self._closed:
self.close()
因此,它关闭套接字(与 Python 2.7.10 相对,那里没有 __exit__ socket 对象的方法)。
我想知道是否:
with open_the_socket() as s:
use s
按预期工作。我在另一个问题上读到,只要套接字的 exit 函数调用关闭,它就可以工作。据说 2.7 没有,但我正在使用 3.4,我只是想知道。
这是来自 Python 3.4.0 的 socket.py 的片段:
def __exit__(self, *args):
if not self._closed:
self.close()
因此,它关闭套接字(与 Python 2.7.10 相对,那里没有 __exit__ socket 对象的方法)。