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

时间:2021-08-19 00:34:15

Quoting this answer:

引用这个答案:

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.

This makes sense to me. But why does Django use tuples and not lists for settings? Example:

这对我来说很有意义。但是为什么Django使用元组而不是设置列表呢?例:

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?

这个(以及所有其他设置)在语义上是不是一个完美的案例列表?

3 个解决方案

#1


10  

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.)

基于user1474837在此问题上对Django票证的有用链接,似乎很明显,元组用于向后兼容设置从一开始就完成的设置,这与元组有关,因为他们相信它们比列表更快。 (根据票证讨论中引用的数据,它们是,但只是非常轻微。)

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.

请注意原始文档中的“纯粹”一词 - 如果采用面值,则表示设置是不可变的,这不是使用元组的原因。还要注意票证讨论中的某些人将设置称为“排序”不可变,因此甚至不清楚设置实际上是不可变的。

P.S. For interest, note the ticket resolution ends with:

附:如有兴趣,请注意门票解决方案的结尾是:

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使用元组,但没有把它作为推荐。这可能会阻止无休止的元组与列表辩论。

#2


10  

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. All settings that were formerly tuples are now lists.

django.conf.global_settings中的默认设置是列表和元组的组合。以前为元组的所有设置现在都是列表。

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

#3


2  

I think the part of reason is tuple is read-only which will more safe and more suitable for setting.

我认为理由的一部分是元组是只读的,它更安全,更适合设置。

#1


10  

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.)

基于user1474837在此问题上对Django票证的有用链接,似乎很明显,元组用于向后兼容设置从一开始就完成的设置,这与元组有关,因为他们相信它们比列表更快。 (根据票证讨论中引用的数据,它们是,但只是非常轻微。)

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.

请注意原始文档中的“纯粹”一词 - 如果采用面值,则表示设置是不可变的,这不是使用元组的原因。还要注意票证讨论中的某些人将设置称为“排序”不可变,因此甚至不清楚设置实际上是不可变的。

P.S. For interest, note the ticket resolution ends with:

附:如有兴趣,请注意门票解决方案的结尾是:

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使用元组,但没有把它作为推荐。这可能会阻止无休止的元组与列表辩论。

#2


10  

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. All settings that were formerly tuples are now lists.

django.conf.global_settings中的默认设置是列表和元组的组合。以前为元组的所有设置现在都是列表。

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

#3


2  

I think the part of reason is tuple is read-only which will more safe and more suitable for setting.

我认为理由的一部分是元组是只读的,它更安全,更适合设置。