[英]Why does Django use tuples for settings and not lists?
Quoting this answer : 引用这个答案 :
Apart from tuples being immutable there is also a semantic distinction that should guide their usage. 除了元组是不可变的之外,还有一个语义上的区别应该指导它们的使用。 Tuples are heterogeneous data structures (ie, their entries have different meanings), while lists are homogeneous sequences. 元组是异构数据结构(即,它们的条目具有不同的含义),而列表是同构序列。 Tuples have structure, lists have order. 元组有结构,列表有顺序。
This makes sense to me. 这对我来说很有意义。 But why does Django use tuples and not lists for settings? 但是为什么Django使用元组而不是列表进行设置? 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? 这个(以及所有其他设置)在语义上是不是一个完美的案例列表?
This was changed in Django 1.9: 这在Django 1.9中有所改变:
Default settings that were tuples are now lists 作为元组的默认设置现在是列表
The default settings in django.conf.global_settings were a combination of lists and tuples. django.conf.global_settings中的默认设置是列表和元组的组合。 All settings that were formerly tuples are now lists. 以前为元组的所有设置现在都是列表。
https://docs.djangoproject.com/en/1.9/releases/1.9/#default-settings-that-were-tuples-are-now-lists https://docs.djangoproject.com/en/1.9/releases/1.9/#default-settings-that-were-tuples-are-now-lists
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. 基于user1474837在这个问题上对Django票证的有用链接,似乎很明显,元组用于向后兼容从一开始就完成设置的方式,这与元组有关,因为他们相信它们比列表更快。 (They are, but only very slightly, according to data cited in the ticket discussion.) (根据票证讨论中引用的数据,它们是,但只是非常轻微。)
Specifically, Django docs used to say: 具体来说,Django文档曾经说过:
For settings that are sequences, use tuples instead of lists. 对于序列设置,请使用元组而不是列表。 This is purely for performance. 这纯粹是为了表现。
Later in the discussion, a Django core developer notes: 稍后在讨论中,Django核心开发人员指出:
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. 另请注意,故障单讨论中的某些人将设置称为“排序”不可变,因此甚至不清楚设置实际上是不可变的。
PS For interest, note the ticket resolution ends with: PS如有兴趣,请注意机票解决方案以:
Changed the "write your own settings" recommendation to mention that Django uses tuples, but not making it a recommendation. 改变了“编写你自己的设置”建议,提到Django使用元组,但没有把它作为推荐。 That might head off the endless tuples vs. lists debates. 这可能会阻止无休止的元组与列表辩论。
我认为原因部分是元组是只读的,它更安全,更适合设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.