Prev: Getting started with python on macintosh snow leopard with mysql - need help
Next: Errno 9] Bad file descriptor
From: Steven D'Aprano on 11 Jul 2010 20:24
On Sun, 11 Jul 2010 08:59:06 -0700, 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, but that rules out
> constructs like:
Yes. So what? Where's the problem?
List methods work in place. If you're annoyed now, that's *nothing* to
the annoyance you'll feel if they returned the list and you did this:
alist = [1,2,3]
blist = alist.append(4) # Make a new list with 4 appended.
assert alist == [1,2,3]
> 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].
L = []
L = [1,2,3,4]
Not everything needs to be a one-liner.