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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With