python - 这条长长的 Python 行的正确 PEP8 做法是什么?

标签 python python-2.7 pep8

我应该如何分解这条线以使其遵守 PEP8?

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

最佳答案

如果您将内容重写为单独的行,则永远不会出现。

它还允许您为中间值指定有意义的名称(我不得不猜测,但想必您知道它们),甚至可以将一些逻辑重构为函数(您也可以给有意义的名字)。

例如,不更改任何逻辑,甚至不重写任何逻辑(除了使用 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()

关于python - 这条长长的 Python 行的正确 PEP8 做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15711209/

相关文章:

Python嵌套for循环变量 'overwrite'

python - 如何为我的数据创建三个新列?

python - 使用lxml解析XML,当有另一个子节点时无法获取文本

python - 如何根据PEP8拆分多个命令?

python - PEP8 插件破坏了我在 Sublime 3 中的类型提示代码

python - 优胜美地 : Python MySQLlib issue - 'no suitable image found'

python - 如何让 Sphinx 自动摘要显示实例属性的文档?

python-2.7 - 带有 Python 2 和 3 的 Spatialite

python - 识别使用 Cython 生成的 C 代码中的 C 函数

python - 如何处理长路径名以符合 pep8 要求?