使用with
语句时,是否需要调用imap4
类的close
和logout
方法?
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/