是否可以在Python中将长行分成多行[重复]

本文翻译自:Is it possible to break a long line to multiple lines in Python [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • How can I do a line break (line continuation) in Python? 如何在Python中进行换行(换行)? 8 answers 8个答案

Just like C, you can break a long line into multiple short lines. 就像C一样,你可以将一条长线分成多条短线。 But in Python , if I do this, there will be an indent error... Is it possible? 但是在Python中 ,如果我这样做,会出现缩进错误......是否可能?


#1楼

参考:https://stackoom.com/question/HVRY/是否可以在Python中将长行分成多行-重复


#2楼

If you want to assign a long str to variable, you can do it as below: 如果要将长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! 不要添加任何逗号,否则你会得到一个包含许多strs的元组!


#3楼

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  AND "\
"ORDER BY WHATEVERS DESC"

kinda like that. 有点像那样。

There is a comment like this from acgtyrant , sorry, didn't see that. acgtyrant有这样的评论,抱歉,没有看到。 :/ :/


#4楼

From PEP 8 - Style Guide for Python Code : 从PEP 8 - Python代码风格指南 :

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. 包装长行的首选方法是在括号,括号和括号内使用Python隐含的行继续。 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. 确保适当缩进续行。

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. 在Python代码中,只要约定在本地一致,就允许在二元运算符之前或之后中断。 For new code Knuth's style (line breaks before the operator) is suggested. 对于新代码,建议使用Knuth的样式(操作符之前的换行符)。

Example of explicit line continuation: 显式行继续的示例:

a = '1'   \
    + '2' \
    + '3' \
    - '4'

#5楼

It works in Python too: 它也适用于Python:

>>> 1+\
      2+\
3
6
>>> (1+
          2+
 3)
6

#6楼

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. 对于三引号字符串( """like this""" ),Python有隐含的行继续(括号内,括号和字符串),并且连续行的缩进并不重要。 For more info, you may want to read this article on lexical analysis, from python.org. 有关更多信息,您可能需要阅读python.org上关于词法分析的这篇文章。

你可能感兴趣的:(python)