简体   繁体   English

为什么Django使用元组设置而不是列表?

[英]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.

相关问题 Django 1.9为什么在设置和URL中用list []替换元组()? - Why did Django 1.9 replace tuples () with lists [] in settings and URLs? 为什么带星号的赋值生成列表而不是元组? - Why does starred assignment produce lists and not tuples? 为什么 copy.deepcopy() 对于元组和列表的行为不同? - Why does copy.deepcopy() behave differently for tuples than lists? 为什么Django的ModelAdmin使用列表而不是元组,反之亦然 - Why Django's ModelAdmin uses lists over tuples and vice-versa 为什么不打印没有重复元组的元组列表? - Why does it not print a list of tuples with no repeated tuples? 为什么Python将元组,列表,集合和字典视为根本不同的东西? - Why does Python treat tuples, lists, sets and dictionaries as fundamentally different things? Python是否使用列表的链接列表?为什么插入慢? - Does Python use linked lists for lists? Why is inserting slow? 在 Python 中,我们可以在列表、元组、集合、字典等数据结构上使用按位运算符吗? 如果是这样,为什么? - In Python can we use bitwise operators on data structures such as lists, tuples, sets, dictionaries? And if so, why? Python 2 如何比较字符串和整数? 为什么列表比数字大,元组比列表大? - How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists? 为什么 numpy 需要元组列表而不是列表列表? - Why numpy requires list of tuples and not list of lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM