[英]How do I merge a tuple or convert a list to a tuple?
I have a tuple, INSTALLED_APPS, and I want to merge extra items into it to get my local apps in the tuple too without altering the main settings file. 我有一个元组INSTALLED_APPS,并且我想将其他项合并到其中,以便在不更改主设置文件的情况下也将我的本地应用程序包含在元组中。 I came this far:
我走了这么远:
DEFAULT_APPS = list(INSTALLED_APPS)
MY_APPS_LIST = DEFAULT_APPS.append('south')
however if I try to convert this into a tuple again by running: 但是,如果我尝试通过运行再次将其转换为元组:
INSTALLED_APPS = tuple(MY_APPS_LIST)
I get: 我得到:
TypeError: 'NoneType' object is not iterable
The question is rather fundamental I'd say, but I can't really find "the" method for this, or even any method that works for me at all... I did find that both list() and tuple() in the Django shell return an empty object of that type, so I don't understand where the TypeError might be coming from... 我会说这个问题是很基本的,但是我找不到真正的“ the”方法,甚至根本找不到对我有用的方法……我确实发现在list()和tuple()中Django外壳返回该类型的空对象,所以我不知道TypeError可能来自何处...
Help appreciated! 帮助赞赏!
append
doesn't return a new list -- it modifies the original list, returning None
. append
不返回新列表-修改原始列表,返回None
。 You want MY_APPS_LIST = DEFAULT_APPS + ['south']
您想要
MY_APPS_LIST = DEFAULT_APPS + ['south']
list
methods operate in-place, and hence return None
. list
方法就地操作,因此返回None
。 If you want to add another element to an existing list and only return the result then just add them. 如果要将另一个元素添加到现有列表中并仅返回结果,则只需添加它们。
MY_APPS_LIST = DEFAULT_APPS + ['south']
note that one can use +
to concatenate tuples, so if INSTALLED_APPS
is a tuple: 请注意,可以使用
+
来连接元组,因此,如果INSTALLED_APPS
是一个元组:
INSTALLED_APPS = DEFAULT_APPS + ('south',)
INSTALLED_APPS
will be a tuple with 'south'
as its last element INSTALLED_APPS
将是一个以'south'
为最后一个元素的元组
The list.append
method does NOT return the list you appended to. list.append
方法不会返回您追加到的列表。 It is an in-place method. 它是就地方法。 You should be doing
你应该在做
DEFAULT_APPS = list(INSTALLED_APPS)
DEFAULT_APPS.append('south')
MY_APPS_LIST = DEFAULT_APPS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.