当函数调用参数列表中的list append元素时,'NoneType'对象不可迭代

时间:2017-11-30 10:57:48

标签: python python-3.x list

我试图将一个元素附加到函数调用中的列表中,该函数调用接受列表作为参数,

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)

解决上述问题。想知道为什么必须这样。

2 个答案:

答案 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作为参数传递。