Why does Django use tuples for settings and not lists?(为什么 Django 使用元组进行设置而不是列表?)
问题描述
引用这个答案:
除了元组是不可变的之外,还有一个语义区别应该指导它们的使用.元组是异构数据结构(即它们的条目具有不同的含义),而列表是同构序列.元组有结构,列表有顺序.
Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.
这对我来说很有意义.但是为什么 Django 使用元组而不是列表进行设置呢?示例:
This makes sense to me. But why does Django use tuples and not lists for settings? Example:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
这(以及所有其他设置)在语义上不是列表的完美案例吗?
Isn't this (and all the other settings) a perfect case semantically for a list?
推荐答案
基于 user1474837 到 Django 的有用链接票 在这个问题上,很明显,元组用于向后兼容从一开始就完成设置的方式,由于相信元组比列表更快,因此使用元组.(根据票务讨论中引用的数据,它们是,但只是非常轻微.)
Based on user1474837's helpful link to the Django ticket on this question, it seems clear that tuples are used for backwards compatibility with the way settings were done from the start, which was with tuples due to the belief they were faster than lists. (They are, but only very slightly, according to data cited in the ticket discussion.)
具体来说,Django 文档曾经说过:
Specifically, Django docs used to say:
对于序列设置,使用元组而不是列表.这纯粹是为了性能.
For settings that are sequences, use tuples instead of lists. This is purely for performance.
在后面的讨论中,一位 Django 核心开发人员指出:
Later in the discussion, a Django core developer notes:
我们当然不会从元组转移到列表,因为它会破坏已经期望事物是元组的现有代码.不过,我会删除性能说明,因为它不值得吓唬人.
We're certainly not about to move from tuples to lists there because it would break existing code that already expects things to be tuples. I'll remove the performance note, however, since it's not worth scaring people.
注意原始文档中的纯粹"一词——如果从表面上看,这意味着表明设置是不可变的,这不是使用元组的原因.另请注意,在票务讨论中有人将设置称为某种"不可变,因此甚至不清楚设置实际上是不可变的.
Note the word "purely" in the original documentation -- which if taken at face value would mean indicating settings are immutable is not a reason tuples are used. Also note someone in the ticket discussion references settings as "sort of" immutable, so it's not even clear settings are in fact immutable.
附:出于兴趣,请注意票务解决方案以:
P.S. For interest, note the ticket resolution ends with:
更改了编写您自己的设置"建议以提及 Django使用元组,但不推荐它.这可能会阻止无尽的元组与列表的辩论.
Changed the "write your own settings" recommendation to mention that Django uses tuples, but not making it a recommendation. That might head off the endless tuples vs. lists debates.
这篇关于为什么 Django 使用元组进行设置而不是列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 Django 使用元组进行设置而不是列表?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01