This is what I just did in python shell:
>>>[1, 2]
...[1, 2]
>>>[1, 2].extend([2,3]) #when I pressed Enter
>>>
#nothing came out
then I tried something else:
>>>l = [1, 2]
>>>l
...[1, 2]
>>>m = [1, 2].extend([2, 3])
>>>m #pressed Enter, nothing came out
>>>m is None
...True
why?
Does it mean, I cannot do something like
>>>m = [...].extend(...)
?
No you can’t,
extend(as append, pop…) modify the object inplace without returning anything.The list objects are mutable objects, they can be modified without need to recreate one.
In your case, you have to do
If you change the type of
l, assume this is another object, let’s call itclass Foo, what would you expect as return value of something likeWon’t you expect something like foo object modified, and nothing returned from the method ?