Reputation: 6221
These work:
>>> print [1,2,3] + [4,5]
[1,2,3,4,5]
>>> 'abc'.upper()
'ABC'
This doesn't:
>>> print [1,2,3].extend([4,5])
None
Why? You can use string methods on bare strings, so why can't you use methods on bare sequence types such as lists? Even this doesn't work:
>>> print list([1,2,3]).extend([4,5])
None
N.B. For a colloquial meaning of 'work'. Of course there'll be a good reason why my expected behaviour is incorrect. I'm just curious what it is.
P.S. I've accepted soulcheck's answer below, and he is right, but on investigating how the addition operator is implemented I just found that the following works:
>>> [1,2,3].__add__([4,5])
[1, 2, 3, 4, 5]
But presumably the add method doesn't modify the underlying object and creates a new one to return, like string methods.
Upvotes: 1
Views: 535
Reputation: 36777
extend
works, just doesn't return any value as it's modifying the object it works on. That's a python convention to not return any value from methods that modify the object they're called on.
Try:
a = [1,2,3]
a.extend([4,5])
print a
edit:
As for why one reason for this could be that it's sometimes ambigous what the method should return. Should list.extend return the extended list? A boolean? New list size?
Also it's an implementation of command-query separation, ie. if it returned something it would be a query and command in one and cqs says it's evil (although sometimes handy - see list.pop()
).
So generally you shouldn't return anything from mutators, but as with almost everything in python it's just a guideline.
list.__add__()
is the method called when you do + on lists - it returns a new list.
Upvotes: 5
Reputation: 39217
This is due to the design of the interface. Extend does extend the list but does not return any value.
l = [1, 2, 3]
l.extend([4, 5])
print l
Upvotes: 0