gpt4 book ai didi

python - 使用with语句和imaplib时需要调用close、logout

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:07 27 4
gpt4 key购买 nike

使用with语句时,是否需要调用imap4类的closelogout方法?

docs不要提供太多上下文。

Changed in version 3.5: Support for the with statement was added.

在另一个,非with example , 他们调用这两种方法。

这样做吗?

with imaplib.IMAP4_SSL(IMAP) as imap:
# Do some stuff
...
imap.close()
imap.logout()

还是只是这个?

with imaplib.IMAP4_SSL(IMAP) as imap:
# Do some stuff
...

最佳答案

IMAP4.__exit__ 调用logout:

def __exit__(self, *args):  # from imaplib.py
try:
self.logout()
except OSError:
pass

所以你只需要调用 close(在 with block 之外):

with imaplib.IMAP4_SSL(IMAP) as imap:
# Do some stuff
imap.close()

关于python - 使用with语句和imaplib时需要调用close、logout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com