在Python中用长类/属性名换行

2024-03-28 14:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把以下几行写下来,使之符合政治公众人物的要求,我相信它们都是有效的。我听说有些人根本不推荐反斜杠,哪一个对你来说不那么难看?在

1

    bake_occ_static_pass.compute_behavior = (
        bake_occ_static_pass.original_compute_behavior)

^{pr2}$

提前谢谢。在


Tags: staticpass政治公众computeoriginal斜杠人物
3条回答

根据PEP8

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.

只有当反斜杠会导致语法问题或使用括号(例如withassert语句)时才会使用反斜杠。e、 g

with something('foo') as some_name, \
    something_else('bar') as some_other_name:

应优先于:

^{pr2}$

PEP似乎更喜欢用圆括号而不是反斜杠,在这种情况下,考虑到行中没有其他圆括号使其更加混乱,我建议使用括号而不是反斜杠。在

1优先。引用神的话,又名PEP8

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.

即使对于importPEP 328也添加了括号形式,如

from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
    LEFT, DISABLED, NORMAL, RIDGE, END)

比…漂亮

^{pr2}$

相关问题 更多 >