append on lists
Armin
a at nospam.org
Tue Sep 16 09:12:27 EDT 2008
Maric Michaud wrote:
> Le Tuesday 16 September 2008 14:23:25 Armin, vous avez écrit :
>> Alex Marandon wrote:
>>> Armin wrote:
>>>> Duncan Booth wrote:
>>>>
>>>> The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
>>>> (with c = [8,9]) is identical,
>>> No it's not, + doesn't alter its operands.
>>>
>>> >>> a = 1
>>> >>> b = 2
>>> >>> a + b
>>>
>>> 3
>> That's not the point :)
>
> It is, please try to understand it, in python all expressions that mutate an
> object should return None, it's the case for
>
> l.append(x)
> l.sort()
> l.reverse()
>
> all expressions that return something, return a new object, it's the case for
>
> 1+2
> 1.__add__(2) (which is the same)
> sorted(l)
> l[i:j]
> etc...
>
> there are some noticeable exceptions :
>
> For coding facilities, some APIs could return the modified part of the object,
> ex : it = c.pop()
>
> Returning the modifyied object itself is mostly considered bad style, because
> it doesn't make clear if this the object or a copy.
OK ... That's a good point. Thanks !
--Armin
More information about the Python-list
mailing list