f---ing typechecking
Neil Cerutti
horpner at yahoo.com
Thu Feb 15 11:37:18 EST 2007
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().
However:
>>> [1, 2].extend([3])
>>> [1, 2] += [3]
SyntaxError: augmented assign to list literal or comprehension not possible
> It seems like the '+' operator for lists should accept any
> iterable for the right side argument to be consistent with
> extend() and the '+=' operator.
+= will only perform the operation in place "whenever possible",
so there are objects for which a conversion to .extend wouldn't
work, and you'd get an actual call of the + operation followed by
the assignment operation.
--
Neil Cerutti
I don't know what to expect right now, but we as players have to do what we've
got to do to make sure that the pot is spread equally. --Jim Jackson
More information about the Python-list
mailing list