PEP8悬挂缩进规范

2024-06-16 11:40:41 发布

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

PEP 8关于悬挂缩进:

When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

有没有关于“子论点”的明确文件?例如:

some_method(argument_one, argument_two, argument_three=[
    'parameter_one',
    'parameter_two',
    'parameter_three',
    ])

与之相反的是:

^{pr2}$

最好只链接到官方讨论。在


Tags: theparameterlinebeargumentonepepfollowing
2条回答

我意识到这个问题由来已久,但我想分享我的想法和对我有效的方法,特别是因为我在法律上是盲人。(我专门针对浅嵌套,并尽可能保持函数调用的简单易读。)

为了避免这种潜在的棘手的可读性情况,实际上,我将把列表(或任何创建复杂、混乱的函数调用的项)定义为一个变量,以某种形式明确命名,表示它是临时的:

# Build temp argument to avoid messy function call.
arg3_tmp = [
    'parameter1',
    'parameter2',
    'parameter3',
]
some_method(argument1, argument2, arg3_tmp)

从政治公众人物8的“其他建议”部分:

Compound statements (multiple statements on the same line) are generally discouraged.

考虑到这一建议,您的第二个示例可能更符合pep8样式指南,因为它避免了在同一行中混合方法调用和列表构造。第二个例子读起来也有点简单。在

相关问题 更多 >