The python docs imply that s.extend(t)
and s += t
are equivalent for some mutable sequence s
and iterable t
. I'm surprised to find that they are not equivalent for an immutable sequence. Can someone shed some light on why extend raises an error
>>> s = (1, 2)
>>> s.extend((3,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'extend'
but += does not
>>> s = (1, 2)
>>> s += (3,)
>>> s
(1, 2, 3)
Is there a practical reason why extend
can't create a new tuple the way +=
does?