Python 中是否可以将长行分成多行?

发布于 2024-10-02 10:22:11 字数 160 浏览 11 评论 0原文

就像C 一样,你可以将一条长线分成多条短线。但是在 Python 中,如果我这样做,将会出现缩进错误。 。 是否可以?

Just like C, you can break a long line into multiple short lines. But in Python, if I do this, there will be an indent error... Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤单情人 2024-10-09 10:22:11

来自 PEP 8 - Python 代码风格指南

换行长行的首选方法是使用 Python 的隐含行
括号、方括号和大括号内的延续。如果有必要的话,你
可以在表达式周围添加一对额外的括号,但有时
使用反斜杠看起来更好。确保缩进续行
适当地。

隐式续行示例:

a = (
    '1'
    + '2'
    + '3'
    - '4'
)


b = some_function(
    param1=foo(
        "a", "b", "c"
    ),
    param2=bar("d"),
)

关于二元运算符的换行主题,它继续说道:

几十年来,推荐的风格是在二元运算符之后中断。
但这可能会以两种方式损害可读性:运算符往往会分散在屏幕上的不同列中,并且每个运算符都会从其操作数移开并移动到上一行。

在 Python 代码中,只要约定在本地一致,就允许在二元运算符之前或之后中断。对于新代码,建议使用 Knuth 风格(在运算符之前换行)。

显式续行示例:

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

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.

Example of implicit line continuation:

a = (
    '1'
    + '2'
    + '3'
    - '4'
)


b = some_function(
    param1=foo(
        "a", "b", "c"
    ),
    param2=bar("d"),
)

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 before the operator) is suggested.

Example of explicit line continuation:

a = '1'   \
    + '2' \
    + '3' \
    - '4'
素罗衫 2024-10-09 10:22:11

有不止一种方法可以做到这一点。

1).长篇大论:

>>> def print_something():
         print 'This is a really long line,', \
               'but we can make it across multiple lines.'

2)。使用括号:

>>> def print_something():
        print ('Wow, this also works?',
               'I never knew!')

3)。再次使用 \

>>> x = 10
>>> if x == 10 or x > 0 or \
       x < 100:
       print 'True'

引用 PEP8

长包的首选方式
行是通过使用Python的隐含
圆括号、方括号和大括号内的续行。如果有需要的话,
您可以在表达式周围添加一对额外的括号,但是
有时使用反斜杠看起来更好。确保缩进续行
适当地。破解二进制文件的首选位置
运算符位于运算符之后,而不是之前。

There is more than one way to do it.

1). A long statement:

>>> def print_something():
         print 'This is a really long line,', \
               'but we can make it across multiple lines.'

2). Using parenthesis:

>>> def print_something():
        print ('Wow, this also works?',
               'I never knew!')

3). Using \ again:

>>> x = 10
>>> if x == 10 or x > 0 or \
       x < 100:
       print 'True'

Quoting 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 after the operator, not before it.

淡淡離愁欲言轉身 2024-10-09 10:22:11

如果你想给变量分配一个长字符串,你可以这样做:

net_weights_pathname = (
    '/home/acgtyrant/BigDatas/'
    'model_configs/lenet_iter_10000.caffemodel')

不要添加任何逗号,否则你会得到一个包含很多字符串的元组!

If you want to assign a long string to variable, you can do it as below:

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 strings!

廻憶裏菂餘溫 2024-10-09 10:22:11

它也适用于 Python:

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

It works in Python too:

>>> 1+\
      2+\
3
6
>>> (1+
          2+
 3)
6
自由如风 2024-10-09 10:22:11

当尝试输入连续文本(例如查询)时,不要在行尾放置逗号,否则您将得到一个字符串列表而不是一个长字符串:

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"

有点像那样。

acgtyrant 有这样的评论,抱歉,没看到。 :/

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. :/

凉城已无爱 2024-10-09 10:22:11

与数据库相关的代码在多行中看起来更容易,并用一对三引号括起来:

SQL = """SELECT
            id, 
            fld_1, 
            fld_2, 
            fld_3, 
            ...... 
         FROM some_tbl"""

比下面的一行巨长的代码更容易看:

SQL = "SELECT id, fld_1, fld_2, fld_3, .................................... FROM some_tbl"

DB related code looks easier on the eyes in multiple lines, enclosed by a pair of triple quotes:

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"
水溶 2024-10-09 10:22:11

据我所知,这是可以做到的。 Python 对于三引号字符串("""like this""")具有隐式续行(在括号、方括号和字符串内),并且续行的缩进并不重要。有关更多信息,您可能需要阅读关于 Python 词法分析的这篇文章。组织。

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 information, you may want to read this article on lexical analysis, from python.org.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文