我试图将一个元素附加到函数调用中的列表中,该函数调用接受列表作为参数,
def print_options(title, option_list):
print('\n' + title)
for index, elem in enumerate(option_list):
print(index, '-', elem)
options = ['A', 'B', 'C']
print_options('Title', options.append('D'))
但是
失败了TypeError: 'NoneType' object is not iterable
必须做
options.append('D')
print_options('Title', options)
解决上述问题。想知道为什么必须这样。
答案 0 :(得分:2)
append
就位并返回None。
这意味着python中的append()
- 函数只会更改对象本身但不返回任何内容。这就是为什么你不能直接在funciton调用中使用它。
答案 1 :(得分:2)
SA
而不是>>> help(list.append)
Help on method_descriptor:
append(...)
L.append(object) -> None -- append object to end
,您将list
作为参数传递。