Django 1.9为什么在设置和URL中用list []替换元组()? [英] Why did Django 1.9 replace tuples () with lists [] in settings and URLs?

查看:80
本文介绍了Django 1.9为什么在设置和URL中用list []替换元组()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么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):

在用于创建自己的设置的文档中有一个 建议显示为对于序列设置,请使用 元组而不是列表.这纯粹是为了提高性能."

In the documentation for ​Creating your own settings there's a recommendation which reads "For settings that are sequences, use tuples instead of lists. This is purely for performance."

这是双层床.分析显示,元组的运行速度不比列表的运行速度快 大多数操作(肯定是循环操作,我们很可能会做最多 经常).另一方面,列表文字语法具有以下优点: 当您只有一个项目时,它不会折叠为一个值,并且 省略尾随逗号,例如元组语法.使用列表语法不是 速度更慢,更清晰,错误更少. 更广泛的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:

我更喜欢它们 [lists] 有两个原因:

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 []替换元组()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆