I use Python like this
>>>print [].append(1)
None
>>>ls = []
>>>ls.append(1)
>>>ls
[1]
why "[].append(1)" value is None ,and other one is real value?
Because the append()
list method doesn't return the list, it just modifies the list it was called on. In this case, an anonymous list is modified and then thrown away.
The documentation isn't super-clear, but all it says is:
list.append(x)
Add an item to the end of the list; equivalent to
a[len(a):] = [x]
.
For other methods, such as list.count(x)
, the word "return" occurs in the description, implying that if it doesn't, the method doesn't have a return value.
append()
does not return the list it modifies, but
Append the object item at the end of list list. Return 0 if successful; return -1 and set an exception if unsuccessful.