Why is it a mistake to assign the result of list.append()?
- None
- append
- list
New users will often write code like:
seq = []
for i in range(10):
seq = seq.append(i)
… And become confused when it crashes with AttributeError: 'NoneType' object has no attribute 'append'
.
They assume that list.append
returns a reference to the original list, but it actually returns None. The solution is to call .append
without assigning the result to anything.
seq = []
for i in range(10):
seq.append(i)