简体   繁体   English

Django 1.9为什么在设置和URL中用list []替换元组()?

[英]Why did Django 1.9 replace tuples () with lists [] in settings and URLs?

I am bit curious to know why Django 1.9 replaced tuples () with lists [] in settings, URLs and other configuration files 我很好奇,为什么Django 1.9为什么在设置,URL和其他配置文件中用list []替换了tuples()

I just upgraded to Django 1.9 and noticed these changes. 我刚刚升级到Django 1.9,并注意到了这些更改。 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 urls.py

urlpatterns = [
    url(r'^', admin.site.urls),
]

Is anything different because of these changes? 这些变化有什么不同吗?

It is explained in issue #8846 (emphasis mine): 在问题#8846 (强调我的问题)中对此进行了解释:

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." 在创建自己的设置的文档中,有一条建议显示为``对于序列设置,请使用元组而不是列表。这纯粹是为了提高性能''。

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. 在更广泛的Python社区中,经常表达的观点似乎是,不应将元组视为不可变的列表。 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. 另请参阅此答案以获取最新讨论。

Another answer (not directly related to this issue) demonstrates that accessing elements is actually faster with a list . 另一个答案 (与该问题没有直接关系)表明, 使用 list更快地访问元素

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 : 在核心Django开发人员Aymeric Augustin 对django开发人员进行以下讨论之后,触发了实际的实现决策:

I prefer them [lists] for two reasons: 我更喜欢它们[列表]有两个原因:

1) All these settings are sequences of similar things. 1)所有这些设置都是类似的序列。 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.) (在Python中,元组既是“没有名称的命名元组”,又是“不可变列表”。)

2) Lists aren't prone to the “missing comma in single-item tuple” problem which bites beginners and experienced pythonistas alike. 2)列表不容易出现“单项元组中缺少逗号”的问题,这对初学者和经验丰富的pythonista都是一样的。 Django even has code to defend against this mistake for a handful of settings. Django甚至有一些代码可以通过一些设置来防御此错误。 Search for “tuple_settings” in the source. 在源代码中搜索“ tuple_settings”。

And the switch to lists actually happened in issue #24149 which also referred to the above discussion. 切换到列表实际上发生在问题#24149中 ,该问题也参考了上面的讨论。

In the release notes of 1.9 , there is: 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. 以前是元组的所有设置现在都列出了。

So it appears that it was just done for consistency. 因此看来,这样做只是为了保持一致性。 Both tuples and lists should work fine. 元组和列表都应该可以正常工作。 If you use a tuple with 1 element, remember the comma (1,) because otherwise it's not a tuple but simply an expression in parens. 如果您使用具有1个元素的元组,请记住逗号(1,)因为否则它不是元组,而只是parens中的一个表达式。

As for urlpatterns, those used to be defined using a patterns() function, but that was deprecated in Django 1.8, as a list of url instances works fine. 至于urlpatterns,那些以前是使用patterns()函数定义的,但是在Django 1.8中已弃用,因为url实例列表可以正常工作。 As the function will be removed in the future, it shouldn't be used in new apps and projects. 由于该功能将来会被删除,因此不应在新的应用和项目中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM