f---ing typechecking
Nick Craig-Wood
nick at craig-wood.com
Tue Feb 20 04:30:05 EST 2007
Neil Cerutti <horpner at yahoo.com> wrote:
> On 2007-02-14, Farshid Lashkari <no at spam.com> wrote:
> > Szabolcs Nagy wrote:
> >>>>> L=[1]
> >>>>> L.extend((1,))
> >>>>> L
> >> [1, 1]
> >
> > Are list.extend() and list concatenation supposed to behave
> > differently? I always thought concatenation was just shorthand
> > for calling extend().
>
> They are different. list.extend() mutates the list, returning
> None, while the + operator returns a new, concatenated list.
>
> += on the other hand works very similarly to list.extend().
It does make an inconsistency though...
>>> L=[1]
>>> L+=(1,)
>>> L
[1, 1]
Wheras
>>> L=[1]
>>> L=L+(1,)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "tuple") to list
>>>
Ie
x += a
does not equal
x = x + a
which it really should for all types of x and a
(That is the kind of statement about which I'm sure someone will post
a perfectly reasonable counterexample ;-)
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list