From: dhruvbird on
Why doesn't python's list append() method return the list itself? For
that matter, even the reverse() and sort() methods?
I found this link (http://code.google.com/edu/languages/google-python-
class/lists.html) which suggests that this is done to make sure that
the programmer understands that the list is being modified in place,
but that rules out constructs like:
([1,2,3,4].reverse()+[[]]).reverse()
I want to prepend an empty list to [1,2,3,4]. This is just a toy
example, since I can always do that with [[]]+[1,2,3,4].

Regards,
-Dhruv.
From: Thomas Jollans on
On 07/11/2010 05:59 PM, dhruvbird wrote:
> Why doesn't python's list append() method return the list itself? For
> that matter, even the reverse() and sort() methods?
> I found this link (http://code.google.com/edu/languages/google-python-
> class/lists.html) which suggests that this is done to make sure that
> the programmer understands that the list is being modified in place,

Yes!

> but that rules out constructs like:
> ([1,2,3,4].reverse()+[[]]).reverse()

No!

you can either approach this by imperatively modifying a list in-place:

L = [1,2,3,4]
L.reverse()
L.append([])
L.reverse()

Or you can use a more functional style:

L2 = reversed(reversed([1,2,3,4]) + [[]])

(or ([1,2,3,4][::-1]+[[]])[::-1], if you like that kind of thing)

Imagine list.reverse and list.append *did* return self:

L1 = [1,2,3,4]
L2 = L1.reverse().append([]).reverse()

would you expect, after this code, that (L1 == L2) and (L1 is L2)? I
think it would surprise a lot of people. Better clearly separate
modifying an object and functionally processing an object.


Cheers

Thomas
From: Thomas Jollans on
On 07/11/2010 06:28 PM, Nathan Rice wrote:
> Do list(reversed(list(reversed([1, 2, 3, 4])) + [[]]))
>
> Though TBH sometimes get annoyed at this behavior myself. There are a
> lot of people who are very vocal in support of returning none, and it
> makes sense in some ways. Since reversed returns an iterator though, it
> makes this code horrible and unreadable.
>

ah yes, forgot about that nuance. casting reversed to list. Still, there
is slicing.
From: Antoine Pitrou on
On Sun, 11 Jul 2010 08:59:06 -0700 (PDT)
dhruvbird <dhruvbird(a)gmail.com> wrote:
> Why doesn't python's list append() method return the list itself? For
> that matter, even the reverse() and sort() methods?
> I found this link (http://code.google.com/edu/languages/google-python-
> class/lists.html) which suggests that this is done to make sure that
> the programmer understands that the list is being modified in place,
> but that rules out constructs like:
> ([1,2,3,4].reverse()+[[]]).reverse()
> I want to prepend an empty list to [1,2,3,4]. This is just a toy
> example, since I can always do that with [[]]+[1,2,3,4].

>>> x = [1,2,3,4]
>>> y = [5,6]
>>> x[:0] = y
>>> x
[5, 6, 1, 2, 3, 4]



From: MRAB on
Thomas Jollans wrote:
> On 07/11/2010 05:59 PM, dhruvbird wrote:
>> Why doesn't python's list append() method return the list itself? For
>> that matter, even the reverse() and sort() methods?
>> I found this link (http://code.google.com/edu/languages/google-python-
>> class/lists.html) which suggests that this is done to make sure that
>> the programmer understands that the list is being modified in place,
>
> Yes!
>
>> but that rules out constructs like:
>> ([1,2,3,4].reverse()+[[]]).reverse()
>
> No!
>
> you can either approach this by imperatively modifying a list in-place:
>
> L = [1,2,3,4]
> L.reverse()
> L.append([])
> L.reverse()
>
[snip]
If you want to prepend an empty list in-place, use the .insert method:

L = [1,2,3,4]
L.insert(0, [])