有必要打电话给 close 和 logout 方法 imap4 使用 with 陈述
close
logout
imap4
with
这个 docs 不要提供太多上下文。
在版本3.5中更改:添加了对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 :
IMAP4.__exit__
def __exit__(self, *args): # from imaplib.py try: self.logout() except OSError: pass
因此,您只需在 with 块):
with imaplib.IMAP4_SSL(IMAP) as imap: # Do some stuff imap.close()