【问题标题】:How to write multi-line conditions with flake8?如何用 flake8 编写多行条件?
【发布时间】:2020-04-03 15:56:53
【问题描述】:

我正在使用Flake8 来获取标准化代码。目前,我很难找到任何多行 if 条件的示例。

我拥有的if 语句示例:

if 'name' in names and data and len(data) > MAX_ACCEPTABLE_LENGTH:

我无法更改常量的长度。我尝试做 (cond1 and cond2 and cond3) 并用线条分隔它们,但无法正确处理。

我该怎么做?

【问题讨论】:

  • 你可能想看看this post
  • @ChristianHiricoiu 我查看了上面的帖子和类似的帖子,但找不到专门适用于flake8 linter 的配置。

标签: python-3.x flake8 linter


【解决方案1】:

根据'multiline if statements' 的 PEP 8 风格指南,您可以这样做:

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
    that_is_another_thing):
    # Since both conditions are true, we can frobnicate.
    do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
        and that_is_another_thing):
    do_something()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2013-12-29
    • 2014-02-20
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多