跳到内容

unnecessary-collection-call (C408) | 不必要的集合调用 (C408)

Derived from the flake8-comprehensions linter. (源自 flake8-comprehensions linter。)

修复总是可用的。

作用

检查可以重写为空字面量的多余的 dict(), list()tuple() 调用。

为什么这不好?

不必要调用,例如,dict(),而不是使用空字面量({})。 前者速度较慢,因为必须在全局范围内查找名称 dict 以防它已被重新绑定。

示例

dict()
dict(a=1, b=2)
list()
tuple()

建议改为

{}
{"a": 1, "b": 2}
[]
()

修复安全性

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. (此规则的修复被标记为不安全,因为它在重写调用时有时会删除注释。但在大多数情况下,注释将被保留。)

Options (选项)