some_list = [a,b]和some_list = [a] + b有什么区别?

时间:2020-06-19 05:57:03

标签: python django

在Django中,似乎

urlpatterns = [
    path('admin/', admin.site.urls),
    path('catalog/', include('catalog.urls')),
    path('', RedirectView.as_view(url='/catalog/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('catalog/', include('catalog.urls')),
    path('', RedirectView.as_view(url='/catalog/')),
    static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
]

是不同的。 但是,按照我的想法,some_list = [a,b]和some_list = [a] + b应该是同一对象。 我说得对吗?

2 个答案:

答案 0 :(得分:2)

有区别。

>>> a = 1
>>> b = [2]
>>> [a, b] # build a list with the elements a and b
[1, [2]]
>>> [a] + b # build a list with the element a, then build a new list with all the elements from [a] and b.
[1, 2]

答案 1 :(得分:1)

实际上“ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)”将

return [
    re_path(
        r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), 
        view, 
        kwargs=kwargs
    ),
]

这是一个列表。

因此您的第一个代码将获得如下列表:

[url_paterns, url_paterns, ...]

您的第二个代码将获得如下列表:

[url_paterns, url_paterns, ..., [url_paterns]]