gpt4 book ai didi

python - 在 python 字符串中使用 '\' 标签

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

python字符串中'\'标签有什么用。例如,我必须执行这个命令:

cmd = 'exec ' + cs_bin + ' ' +  \
vmtree.rsplit('-', 1)[1].split('/')[0] + ' --xml ' \
+ tmp_corefile + ' | ' + XML2HTML

'\'有什么用?谢谢。

最佳答案

\ 用于续行。

但是你应该只在需要的时候使用它:

引自PEP-8 :

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. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

IMO 这看起来更好:

cmd = ('exec ' + cs_bin + ' ' +
vmtree.rsplit('-', 1)[1].split('/')[0] + ' --xml ' + tmp_corefile + ' | '
+ XML2HTML)

PEP-8 中也提到了为什么要连续而不是一大行的原因:

Limit all lines to a maximum of 79 characters.

There are still many devices around that are limited to 80 character lines; plus, limiting windows to 80 characters makes it possible to have several windows side-by-side. The default wrapping on such devices disrupts the visual structure of the code, making it more difficult to understand. Therefore, please limit all lines to a maximum of 79 characters. For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.

关于python - 在 python 字符串中使用 '\' 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753250/

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