我目前正在尝试wemake-python-styleguide并发现WPS335:
Using lists, dicts, and sets do not make much sense. You can use tuples instead. Using comprehensions implicitly create a two level loops, that are hard to read and deal with.
它给出了一个例子:
# Correct:
for person in ('Kim', 'Nick'):
...
# Wrong:
for person in ['Kim', 'Nick']:
...
这纯粹是个人偏好,还是有更多的理由支持使用元组?我只能考虑速度,但我无法想象这会有什么不同
我想我已经看到越来越多的人使用列表,我想知道是否有理由改变它
使用列表而不是元组作为常量在CPython中没有区别。对于某些版本,两者都编译为元组
请注意列表文字是如何转换为元组的
LOAD_CONST (('Kim', 'Nick'))
指令的至于偏好,CPython更喜欢
tuple
。如果你有选择权,你也应该这样做相关问题 更多 >
编程相关推荐