python中的\是做什么用的?

What is the \ used for in python?

我正在学习 imaplib 的教程,它使用 .我在任何地方都找不到有关它的任何信息。

stat, dta = msrvr.fetch\ (cnt[0],\ '(UID BODY[TEXT])')

您发布的语法无效。您只能在行尾使用 \

stat, dta = msrvr.fetch\
 (cnt[0],\
 '(UID BODY[TEXT])')

它告诉 Python 期望更多的语法仍然是该行的一部分;它将逻辑线延伸到物理边界。请参阅 Python 参考文档中的 Explicit line joining

不鼓励使用它,第二个 \ 完全是多余的,因为逻辑行也通过使用括号进行了扩展。上面最好写成:

stat, dta = msrvr.fetch(
    cnt[0],
    '(UID BODY[TEXT])')

参见Implicit line joining and the Python Style Guide

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.