unnecessary-generator-list (C400)
Derived from the flake8-comprehensions linter. (源自 flake8-comprehensions linter。)
修复总是可用的。
作用
检查可以重写为列表推导式(或直接使用 list()
)的不必要的生成器。
为什么这不好?
围绕生成器表达式使用 list()
是不必要的,因为这些类型存在等效的推导式。使用推导式更清晰且更符合习惯用法。
此外,如果可以完全删除推导式,例如 list(x for x in foo)
,则最好直接使用 list(foo)
,因为它更直接。
示例
建议改为
修复安全性
This rule's fix is marked as unsafe, as it may occasionally drop comments when rewriting the call. In most cases, though, comments will be preserved. (此规则的修复被标记为不安全,因为它在重写调用时有时会删除注释。但在大多数情况下,注释将被保留。)