为什么 Django 1.9 在设置和 URL 中将元组 () 替换为列表 []?
Why did Django 1.9 replace tuples () with lists [] in settings and URLs?
我有点好奇为什么 Django 1.9 在设置、URL 和其他配置文件中用列表 [] 替换元组 ()
我刚刚升级到 Django 1.9 并注意到了这些变化。他们背后的逻辑是什么?
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),
]
这些变化有什么不同吗?
在问题 #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. They are intended as fixed-length records - indeed
the mathematical concept of a tuple is quite distinct from that of a
sequence.
另请参阅 this answer 了解最新的讨论。
另一个 answer(与此问题没有直接关系)表明 访问元素 实际上使用 list
更快。
更新和更多信息: 上面的问题在几年前就已经关闭了,这是正确的,但我把它包括在内是因为它解释了该决定背后的基本原理,许多类似的讨论参考同一张票。实际执行决定是在 following discussion on django-developers started by core Django developer Aymeric Augustin:
之后触发的
I prefer them [lists] for two reasons:
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) 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,这也参考了上述讨论。
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.
看来这样做只是为了保持一致性。元组和列表都应该可以正常工作。如果您使用包含 1 个元素的元组,请记住逗号 (1,)
,否则它就不是元组,而只是括号中的表达式。
至于 url 模式,那些过去使用 patterns()
函数定义的模式,但在 Django 1.8 中已弃用,因为 url 实例列表工作正常。由于该功能将来会被删除,因此不应在新的应用程序和项目中使用它。
我有点好奇为什么 Django 1.9 在设置、URL 和其他配置文件中用列表 [] 替换元组 ()
我刚刚升级到 Django 1.9 并注意到了这些变化。他们背后的逻辑是什么?
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),
]
这些变化有什么不同吗?
在问题 #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. They are intended as fixed-length records - indeed the mathematical concept of a tuple is quite distinct from that of a sequence.
另请参阅 this answer 了解最新的讨论。
另一个 answer(与此问题没有直接关系)表明 访问元素 实际上使用 list
更快。
更新和更多信息: 上面的问题在几年前就已经关闭了,这是正确的,但我把它包括在内是因为它解释了该决定背后的基本原理,许多类似的讨论参考同一张票。实际执行决定是在 following discussion on django-developers started by core Django developer Aymeric Augustin:
之后触发的I prefer them [lists] for two reasons:
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) 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,这也参考了上述讨论。
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.
看来这样做只是为了保持一致性。元组和列表都应该可以正常工作。如果您使用包含 1 个元素的元组,请记住逗号 (1,)
,否则它就不是元组,而只是括号中的表达式。
至于 url 模式,那些过去使用 patterns()
函数定义的模式,但在 Django 1.8 中已弃用,因为 url 实例列表工作正常。由于该功能将来会被删除,因此不应在新的应用程序和项目中使用它。