问题描述
我很好奇为什么Django 1.9为什么在设置,URL和其他配置文件中用列表[]替换了元组()?
我刚刚升级到Django 1.9,并注意到了这些更改.他们背后的逻辑是什么?
I just upgraded to Django 1.9 and noticed these changes. What is the logic behind them?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
]
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
urls.py
urlpatterns = [
url(r'^', admin.site.urls),
]
由于这些变化有什么不同吗?
Is anything different because of these changes?
推荐答案
在问题#8846 (重点是我的):
It is explained in issue #8846 (emphasis mine):
这是双层床.分析显示,元组的运行速度不比列表的运行速度快 大多数操作(肯定是循环操作,我们很可能会做最多 经常).另一方面,列表文字语法具有以下优点: 当您只有一个项目时,它不会折叠为一个值,并且 省略尾随逗号,例如元组语法.使用列表语法不是 速度更慢,更清晰,错误更少. 更广泛的Python社区似乎不应该考虑元组 作为不可变的列表.它们旨在作为定长记录-实际上 元组的数学概念与 顺序.
This is bunk. Profiling shows that tuples run no faster than lists for most operations (certainly looping, which we are likely to do most often). On the other hand, list-literal syntax has the advantage that it doesn't collapse to a single value when you have a single item and omit the trailing comma, like tuple syntax. Using list syntax is no slower, more legible and less error prone. An often-expressed view in the wider Python community seems that tuples should not be considered as immutable lists. They are intended as fixed-length records - indeed the mathematical concept of a tuple is quite distinct from that of a sequence.
另请参见此答案以获取最新的讨论.
Also see this answer for a more up-to-date discussion.
另一个答案(与该问题没有直接关系)表明访问元素实际上是list
更快.
Another answer (not directly related to this issue) demonstrates that accessing elements is actually faster with a list
.
更新和更多信息:上面的问题几年前已经关闭,这是正确的,但是我将其包括在内是因为它解释了该决定的依据,并且许多类似的讨论都涉及同一票证.在关于django开发人员的讨论之后,触发了实际的实施决策由Django核心开发人员美国奥古斯丁:
Update and further information: It is correct that the above issue was closed years ago, but I included it because it explained the rationale behind the decision and many similar discussions refer to the same ticket. The actual implementation decision was triggered after the following discussion on django-developers started by core Django developer Aymeric Augustin:
1)所有这些设置都是类似的序列.这样的值是 最好用列表表示,除非它们必须是不可变的 在哪种情况下可以使用元组. (元组都是没有元组的命名元组 名称"和不可变列表".)
1) All these settings are sequences of similar things. Such values are best represented with lists, unless they have to be immutable, in which case a tuple can be used. (tuples are both "namedtuples without names" and "immutable lists" in Python.)
2)列表不容易出现单项元组中缺少逗号"的情况 这个问题吸引了初学者和经验丰富的pythonista使用者. Django甚至有一些代码可以防范此错误 设置.在源代码中搜索"tuple_settings".
2) Lists aren’t prone to the "missing comma in single-item tuple" problem which bites beginners and experienced pythonistas alike. Django even has code to defend against this mistake for a handful of settings. Search for "tuple_settings" in the source.
实际上,切换到列表的操作发生在 issue#24149 中,该问题也与上述内容相同讨论.
And the switch to lists actually happened in issue #24149 which also referred to the above discussion.
这篇关于Django 1.9为什么在设置和URL中用list []替换元组()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!