append on lists

Grant Edwards invalid at invalid
Mon Sep 15 16:40:50 EDT 2008


On 2008-09-15, Armin <a at nospam.org> wrote:

>> Because .append() mutates 'a' and appends the item in-place rather
>> than creating and returning a new list with the item appended, and
>> it's good Python style for mutating methods to have no return value
>> (since all functions must have some return value, Python uses None
>> when the function doesn't explicitly return anything).
>
> Yes, but this is very unconvenient.

Aw, admit it -- it's not _that_ inconvenient.

> If d should reference the list a extended with a single list
> element you need at least two lines
>
> a.append(7)
> d=a

With that usage it's obvious that a is mutated, and now both
"a" and "d" are bound to the same extended list.

> and not more intuitive d = a.append(7)

Becase that usage implies (at least to many of us) that "a" is
unchanged, and that "d" is now bound to a different object than
"a".

-- 
Grant Edwards                   grante             Yow! Where's th' DAFFY
                                  at               DUCK EXHIBIT??
                               visi.com            



More information about the Python-list mailing list