python怎么换行不执行

编程入门 行业动态 更新时间:2024-10-11 21:30:11

python怎么<a href=https://www.elefans.com/category/jswz/34/1767013.html style=换行不执行"/>

python怎么换行不执行

我有一行很长的代码,我想在多行中分解。我使用什么,语法是什么?

例如,添加一组字符串,

1e = 'a' + 'b' + 'c' + 'd'

把它分成两行:

1

2e = 'a' + 'b' +

'c' + 'd'

线路是什么?您可以在下一行使用参数,而不会出现任何问题:

1

2a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,

blahblah6, blahblah7)

否则,您可以这样做:

1

2if a == True and \

b == False

有关详细信息,请查看样式指南。

从示例行:

1

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

'4' + '5'

或:

1

2a = ('1' + '2' + '3' +

'4' + '5')

请注意,样式指南指出,最好使用带括号的隐式延续,但在这种特殊情况下,在表达式周围添加括号可能是错误的做法。

实际上,样式指南的首选项是向后的。最好使用隐式延续,只有在必要时才使用显式反斜杠。

我想这是说,如果你已经在一个表达式周围加了括号,那么就使用那些括号,但是不要仅仅为了在多行上打断表达式而在表达式周围加括号。不过,没有硬性规定。不过,我认为对于问题中的这一行来说,反斜杠是可行的。

那不正确。请编辑您的答案以反映样式指南实际所说的内容,而不是相反。

卡尔:我不同意,这是从指南中得出的:包装长线的首选方法是在圆括号、括号和大括号内使用Python的隐含行继续符。如果需要,可以在表达式周围添加一对额外的括号,但有时使用反斜杠会更好。

杰鲁布:你不同意卡尔的观点,但是你引用了指南中的一句话:"首选的方法是……在括号内使用python隐含的行继续符…"。所以你要么不同意哈雷的观点,要么就失去了你所贴引言的意义。

问题是,在这种情况下,不需要括号。当然,您可以拥有它们,但是为什么要将它们放在问题中的示例周围呢?不管怎样,我都会稍微修改一下我的答案来澄清。

样式指南引用的关键部分是"如果需要,可以在表达式周围添加一对额外的圆括号,但有时使用反斜杠会更好。"样式指南并不是说应该添加圆括号,而是让作者自己判断。

大概PEP-8在添加这些注释后发生了变化,因为现在应该添加括号来换行:"通过将表达式换行到括号中,可以在多行上换行。"

@Harleyholcombe我认为可以接受添加括号来换行;如果表达式已经有括号,就不需要换行了。然而,这绝对不是100%清楚,我不会在解释上争论。对我来说,决定性的因素是我认为那样看起来更好:)。

Pep8的确在2010年发生了变化,"有时候用反斜杠看起来更好"已经不复存在了。

@E100"反斜杠有时仍然合适。"现在是2014年吗?不知道它是不是消失了,然后又回来了,或者只是移动了。

不是很重要,但因为它是python,我建议使用(spam + eggs + foo + bar + moreSpam + moreEggs),因为在python中,垃圾邮件和鸡蛋是传统的。

所以,如果你用链子,你应该用帕伦斯绕着你的断线链子?我对Python来自C、C++、C和JavaScript感到非常失望。这并不像我想象的那么简单,因为空白确实很重要,认为这越少越好,但他们得到了我。

如何处理ans['Interfaces Declared by Environment'][env_.name][interface_] = None?这条线太长了,但我不知道该在哪里分开!

如果下面有一个与缩进代码块对齐的悬挂缩进,我的IDE就会冲我大喊大叫。在这些情况下,我经常删除括号,并用反斜杠替换它们,这样就有一个单字符的间隙。

带括号版本的优点:可以在换行符后添加注释。我经常使用多行语句将单个语句拆分为多个部分,并对每个部分进行注释。不能用反斜杠。

从python代码的样式指南:

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.

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:

1

2

3with open('/path/to/some/file/you/want/to/read') as file_1, \

open('/path/to/some/file/being/written', 'w') as file_2:

file_2.write(file_1.read())

Another such case is with assert statements.

Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:

1

2

3

4

5

6

7

8

9

10

11

12

13

14class Rectangle(Blob):

def __init__(self, width, height,

color='black', emphasis=None, highlight=0):

if (width == 0 and height == 0 and

color == 'red' and emphasis == 'strong' or

highlight > 100):

raise ValueError("sorry, you lose")

if width == 0 and height == 0 and (color == 'red' or

emphasis is None):

raise ValueError("I don't think so -- values are %s, %s" %

(width, height))

Blob.__init__(self, width, height,

color, emphasis, highlight)

编辑:PEP8现在建议数学家和他们的出版商使用相反的约定(用于打破二进制运算),以提高可读性。

DonaldKnuth在二元运算符垂直对齐之前的中断方式,从而在确定添加和减去哪些项时减少了眼睛的工作量。

来自PEP8:在二元运算符之前还是之后换行?:

Donald Knuth explains the traditional rule in his Computers and Typesetting series:"Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"[3].

Following the tradition from mathematics usually results in more readable code:

1

2

3

4

5

6# Yes: easy to match operators with operands

income = (gross_wages

+ taxable_interest

+ (dividends - qualified_dividends)

- ira_deduction

- student_loan_interest)

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 is suggested.

【3】:唐纳德·克努斯的《特克斯本》,第195和196页。

注意,2010年的建议变了:"长线可以被打破……将表达式括在括号中。这些应该优先使用反斜杠…",并且所有反斜杠都从代码示例中删除。

@E100:读上面的粗体字:The preferred way .. is by using Python's implied line continuation inside parentheses和by wrapping expressions in parentheses一样。我更新了示例

但请注意,"有时使用反斜杠看起来更好"也已经不复存在了。

@E100:这里有三个代码示例,反斜杠使代码更易读:"有时候样式指南不适用。如果有疑问,请运用最佳判断。"

2015年,由于可读性的提高,样式指南在Donald Knuth研究后被更新为更倾向于在二元运算符之前中断。

使用反斜杠结束一行的危险是,如果在反斜杠后面加上空格(当然,很难看到),反斜杠就不再像你想象的那样做了。

有关更多信息,请参见python惯用法和反惯用法(针对python 2或python 3)。

这是能够更好地看到尾随空格的一个原因;也就是说,在vim中有类似set list listchars=trail:·的内容。:)

可以在括号和大括号之间换行。此外,还可以将反斜杠字符\附加到一行,以显式中断该行:

1

2

3

4x = (tuples_first_value,

second_value)

y = 1 + \

2

在行尾放一个\,或将声明附在parens ( .. )中。来自IBM:

1

2

3b = ((i1 < 20) and

(i2 < 30) and

(i3 < 40))

1

2

3b = (i1 < 20) and \

(i2 < 30) and \

(i3 < 40)

From the horse's mouth: Explicit line

joining

Two or more physical lines may be

joined into logical lines using

backslash characters (\), as follows:

when a physical line ends in a

backslash that is not part of a string

literal or comment, it is joined with

the following forming a single logical

line, deleting the backslash and the

following end-of-line character. For

example:

1

2

3

4if 1900 < year < 2100 and 1 <= month <= 12 \

and 1 <= day <= 31 and 0 <= hour < 24 \

and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date

return 1

A line ending in a backslash cannot

carry a comment. A backslash does not

continue a comment. A backslash does

not continue a token except for string

literals (i.e., tokens other than

string literals cannot be split across

physical lines using a backslash). A

backslash is illegal elsewhere on a

line outside a string literal.

-1因为这个例子是单变量的IMO,所以复合条件可以完全用括号代替,这更实用(用于编辑或自动重新排序),而且是惯用的。

可能不是pythonic方法,但我通常使用带有join函数的list来编写像SQL查询这样的长字符串。

1

2

3

4

5

6query ="".join([

'SELECT * FROM"TableName"',

'WHERE"SomeColumn1"=VALUE',

'ORDER BY"SomeColumn2"',

'LIMIT 5;'

])

摘自《Python漫游指南》(续行):

When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.

A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.

However, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.

也就是说,这里有一个关于多重进口的例子(当超过PEP-8中定义的行限制时):

1

2

3from app import (

app, abort, make_response, redirect, render_template, request, session

)

更多推荐

python怎么换行不执行

本文发布于:2024-03-12 21:40:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:换行   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!