【问题标题】:Is it allowed to write if-statements as one-liner? [closed]是否允许将 if 语句写为单行? [关闭]
【发布时间】:2018-03-29 08:10:57
【问题描述】:

关于换行符的 if 语句的推荐编写方法是什么?

显然有两种可能性。第一个来自Google's Python Style Guide,第二个是换行符。

if foo: bar(foo)

对

if foo:
    bar(foo)

对于现有指南或PEPs(以避免基于意见的讨论),应该首选哪种变体?

【问题讨论】:

标签: python pep


【解决方案1】:

来自PEP 8:

一般不鼓励复合语句(同一行上的多个语句)。

[...]

而不是:

if foo == 'blah': do_blah_thing()

do_one(); do_two(); do_three()

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2014-08-19
    • 2014-10-21
    • 2014-11-23
    • 2022-07-20
    • 2020-12-27
    • 2021-12-16
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多