将一行python拆分为多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14417571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Breaking a line of python to multiple lines?
提问by MintyAnt
In C++, I like to break up my lines of code if they get too long, or if an if statement if there are a lot of checks in it.
在 C++ 中,如果代码行太长,或者如果其中有很多检查,我喜欢拆分 if 语句。
if (x == 10 && y < 20 && name == "hi" && obj1 != null)
// Do things
// vs
if (x == 10
&& y < 20
&& name == "hi"
&& obj1 != null)
{
// Do things
}
AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems, mAppliedEffects[inEffectIDHash], inTagNameHash);
// vs
AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems,
mAppliedEffects[inEffectIDHash], inTagNameHash);
In Python, code blocks are defined by the tabs, not by the ";" at the end of the line
在 Python 中,代码块由选项卡定义,而不是由“;” 在行尾
if number > 5 and number < 15:
print "1"
Is mutliple lines possible in python? like...
python中可以有多行吗?喜欢...
if number > 5
and number < 15:
print "1"
I don't think this is possible, but it would be cool!
我不认为这是可能的,但这会很酷!
采纳答案by ATOzTOA
Style guide says:
风格指南说:
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.
包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行续行。通过将表达式括在括号中,可以将长行分成多行。这些应该优先于使用反斜杠进行行延续。确保适当缩进连续行。打破二元运算符的首选位置是在运算符之后,而不是在它之前。
Method 1: Using parenthesis
方法 1:使用括号
if (number > 5 and
number < 15):
print "1"
Method 2: Using backslash
方法 2:使用反斜杠
if number > 5 and \
number < 15:
print "1"
Method 3: Using backslash + indent for better readability
方法 3:使用反斜杠 + 缩进以获得更好的可读性
if number > 5 and \
number < 15:
print "1"
回答by Vladimir
You can place \symbol to escape end-of-line:
您可以放置\符号以转义行尾:
if number > 5 \
and number < 15:
print '1'
In some cases (e.g. inside parentheses) you'll need no special symbols to escape end of line.
在某些情况下(例如在括号内),您不需要特殊符号来转义行尾。
See more in documentation on python lexical analysis:
在关于 python 词法分析的文档中查看更多信息:
The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.
逻辑行的结尾由标记 NEWLINE 表示。语句不能跨越逻辑行边界,除非语法允许 NEWLINE(例如,复合语句中的语句之间)。一条逻辑线是由一条或多条物理线按照显式或隐式连接规则构成的。
回答by David Robinson
You can break an expression up into multiple lines if you surround it with parentheses:
如果用括号将表达式括起来,则可以将表达式分成多行:
if (x == 10
and y < 20
and name == "hi"
and obj1 is not None):
# do something
The same is true of brackets or curly braces used to create a list or dictionary:
用于创建列表或字典的方括号或花括号也是如此:
mylist = [1, 2, 3, 4,
5, 6, 7, 8]
mydict = {1: "a", 2: "b",
3: "c", 4: "d"}
回答by JonathanV
The pep8 standard guide seems to indented new lines for a list of things in parenthesis while for a long line they suggest a backslash at the end of the line.
pep8 标准指南似乎为括号中的内容列表缩进了新行,而对于长行,他们建议在行尾添加反斜杠。

