是否可以在 Python 中将长行拆分为多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4172448/
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
Is it possible to break a long line to multiple lines in Python
提问by Bin Chen
采纳答案by Darin Dimitrov
From PEP 8 - Style Guide for Python Code:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.
包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐式续行。如有必要,您可以在表达式周围添加一对额外的括号,但有时使用反斜杠看起来更好。确保适当缩进连续行。
Example of implicit line continuation:
隐式续行示例:
a = some_function(
'1' + '2' + '3' - '4')
On the topic of line-breaks around a binary operator, it goes on to say:-
关于围绕二元运算符换行的话题,它继续说:-
For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line.
In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style (line breaks beforethe operator) is suggested.
几十年来,推荐的风格是在二元运算符之后打破。但这会在两个方面损害可读性:运算符往往分散在屏幕上的不同列中,并且每个运算符都从其操作数移到前一行。
在 Python 代码中,只要约定在本地保持一致,就可以在二元运算符之前或之后中断。对于新代码,建议使用 Knuth 的样式(运算符前的换行符)。
Example of explicit line continuation:
显式续行示例:
a = '1' \
+ '2' \
+ '3' \
- '4'
回答by Abyx
It works in Python too:
它也适用于 Python:
>>> 1+\
2+\
3
6
>>> (1+
2+
3)
6
回答by Michael Foukarakis
As far as I know, it can be done. Python has implicit line continuation (inside parentheses, brackets, and strings) for triple-quoted strings ("""like this""")and the indentation of continuation lines is not important. For more info, you may want to read thisarticle on lexical analysis, from python.org.
据我所知,是可以做到的。Python 对三重引号字符串 ( """like this""")具有隐式续行(在括号、方括号和字符串内),并且续行的缩进并不重要。有关更多信息,您可能需要阅读python.org 上有关词法分析的这篇文章。
回答by user225312
There is more than one way to do it.
有不止一种方法可以做到这一点。
1). A long statement:
1)。一个长声明:
>>> def print_something():
print 'This is a really long line,', \
'but we can make it across multiple lines.'
2). Using parenthesis:
2)。使用括号:
>>> def print_something():
print ('Wow, this also works?',
'I never knew!')
3). Using \again:
3)。\再次使用:
>>> x = 10
>>> if x == 10 or x > 0 or \
x < 100:
print 'True'
Quoting PEP8:
引用PEP8:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is afterthe operator, not before it.
包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行续行。如有必要,您可以在表达式周围添加一对额外的括号,但有时使用反斜杠看起来更好。确保适当缩进连续行。打破二元运算符的首选位置是在运算符之后,而不是在它之前。
回答by acgtyrant
If you want to assign a long str to variable, you can do it as below:
如果要将 long str 分配给变量,可以按如下方式进行:
net_weights_pathname = (
'/home/acgtyrant/BigDatas/'
'model_configs/lenet_iter_10000.caffemodel')
Do not add any comma, or you will get a tuple which contains many strs!
不要添加任何逗号,否则您将得到一个包含许多字符串的元组!
回答by kotbeg
When trying to enter continuous text (say, a query) do not put commas at the end of the line or you will get a list of strings instead of one long string:
当尝试输入连续文本(例如查询)时,不要在行尾放置逗号,否则您将得到一个字符串列表而不是一个长字符串:
queryText= "SELECT * FROM TABLE1 AS T1"\
"JOIN TABLE2 AS T2 ON T1.SOMETHING = T2.SOMETHING"\
"JOIN TABLE3 AS T3 ON T3.SOMETHING = T2.SOMETHING"\
"WHERE SOMETHING BETWEEN <WHATEVER> AND <WHATEVER ELSE>"\
"ORDER BY WHATEVERS DESC"
kinda like that.
有点像那样。
There is a comment like this from acgtyrant, sorry, didn't see that. :/
有这样的评论来自acgtyrant,抱歉,没看到。:/
回答by Down the Stream
DB related code looks easier on the eyes in multiple lines, enclosed by a pair of triple quotes:
DB 相关代码在多行中看起来更容易,用一对三引号括起来:
SQL = """SELECT
id,
fld_1,
fld_2,
fld_3,
......
FROM some_tbl"""
than the following one giant long line:
比下面一条巨大的长线:
SQL = "SELECT id, fld_1, fld_2, fld_3, .................................... FROM some_tbl"

