简体   繁体   English

对于这条较长的Python行,PEP8的正确做法是什么?

[英]What's the correct PEP8 practice for this long Python line?

How should I break down this line so it abides by PEP8? 我应该如何打破这条线,使其遵守PEP8?

    assert (sum(map(lambda x: len(x), 
                    (activities,apps,classes,users,verbs))) ==
            Object.query
                  .filter(Object.status != ObjectStatusChoices.DELETED)
                  .count())

If you rewrite things into separate lines, this never comes up. 如果将内容重写为单独的行,则永远不会出现。

It also lets you give meaningful names to the intermediate values (which I had to guess at, but presumably you know them), or even refactor bits of logic out into functions (which you can also give meaningful names to). 它也可以让你给有意义的名称,以中间值(这是我在猜测,但想必你认识他们),甚至重构逻辑位伸到功能(你可以给有意义的名称)。

For example, not changing any of your logic, or even rewriting any of it (except to use len in place of lambda x: len(x) ): 例如,不更改任何逻辑,甚至不重写任何逻辑(除了使用len代替lambda x: len(x) ):

lengths = map(len, (activities,apps,classes,users,verbs))
db_query = Object.query.filter(Object.status != ObjectStatusChoices.DELETED)
assert sum(lengths) == db_query.count()

First of all, keep in mind that there is no single "correct PEP8 answer" to this. 首先,请记住,对此没有单个“正确的PEP8答案”。 I prefer: 我更喜欢:

in_mem = sum(len(x) for x in (activities,apps,classes,users,verbs))
in_db = Object.query.filter(
                        Object.status != ObjectStatusChoices.DELETED
                        ).count()
assert in_mem == in_db

Definitely if you find yourself with a statement that needs to be split onto five lines, you probably want more statements. 当然,如果您发现自己需要将一条语句分为五行,则可能需要更多语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM