From: dhruvbird on 11 Jul 2010 14:07 On Jul 11, 9:19 pm, Thomas Jollans <tho...(a)jollans.com> 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() > > Or you can use a more functional style: > > L2 = reversed(reversed([1,2,3,4]) + [[]]) Okay, but this assumes that I have reversed/sorted/etc... type of functions for all member functions that mutate the container. Also, as Nathan mentioned, reversed returns an iterator, whereas sorted returns a list. This asymmertic behaviour is a bit unnerving. > > (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. I think this is a fair call. Honestly, I wouldn't expect them to be the same. However, there are cases when I want to be able to write down my intent in one line. Much like f(g(h(x))). On a side note, is there any other way to append to a list using slices (apart from the one below): x[len(x):len(x)] = [item to append] And while we are talking about python here, why does this statement: y = x[:0] = [100] behave the way it does? I mean everything except for the last value is assigned to the last value rather than the assignments following the chain and every item getting its succeeding item's reference? Regards, -Dhruv.
From: News123 on 11 Jul 2010 20:30 dhruvbird wrote: > > On a side note, is there any other way to append to a list using > slices (apart from the one below): > x[len(x):len(x)] = [item to append] dy you mean x.extend([1,2,3]) ?
From: Raymond Hettinger on 11 Jul 2010 20:55 On Jul 11, 8:59 am, dhruvbird <dhruvb...(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? Because Guido thinks that having those methods return None is the best way to communicate that the underlying object has been mutated in- place. Some other languages do it differently, but this is Guido's language, so we do it his way. Raymond
From: Stephen Hansen on 12 Jul 2010 01:10 On 7/11/10 10:03 PM, Nathan Rice wrote: > Yeah, I long ago filed the in place place in the same folder as > strings-as-sequences, all() returning True for an empty iterable and any > returning True rather than the thing which triggered it. You know, the latter two I can see an argument for, and could see the usefulness therein -- though I've never used either like that, but I consider that chance. I could see the use (and could readily write my own all/any in such a case, then keep it in my toolbox). But the first: what?! for ch in data: is exceptionally useful. Strings-as-sequences I've used hundreds, thousands of times. I use it constantly. -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Chris Rebert on 12 Jul 2010 01:50 On Sun, Jul 11, 2010 at 10:03 PM, Nathan Rice <nathan.alexander.rice(a)gmail.com> wrote: > Yeah, I long ago filed the in place place in the same folder as <snip> > all() returning True for an empty iterable If you weren't taught about vacuous truth (or even identity elements) in Discrete Mathematics, someone fscked up. Said behavior is the absolute correct behavior from a formal logic standpoint. Cheers, Chris -- http://blog.rebertia.com
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: I wish for a tool named "2to6". Next: Easy questions from a python beginner |