python - why [].append(1) is None -
this question has answer here:
i use python this
>>>print [].append(1) none >>>ls = [] >>>ls.append(1) >>>ls [1]
why "[].append(1)" value none ,and other 1 real value?
because append()
list method doesn't return list, modifies list called on. in case, anonymous list modified , thrown away.
the documentation isn't super-clear, says is:
list.append(x)
add item end of list; equivalent
a[len(a):] = [x]
.
for other methods, such list.count(x)
, word "return" occurs in description, implying if doesn't, method doesn't have return value.
Comments
Post a Comment