Why not Lisp-like list-related functions ?
Hello all, I'm new to Python but quite familiar with Lisp. So my question is about Python list-related functions. Why append(), extend(), sort(), reverse() etc. doesn't return a reference to it's own (modified) argument ? IMHO (I'm tweaking Python 2.1 to allow first example possible),
[3 + x * 2 for x in [5, 8, 9, 3].sort()].extend([6, 3, 8].reverse()) [9, 13, 19, 21, 8, 3, 6]
looks much better (and more "functional") than
x = [5, 8, 9, 3] x.sort() x = [3 + x * 2 for x in x] y = [6, 3, 8] y.reverse() x.extend(y) x [9, 13, 19, 21, 8, 3, 6]
Python designers and fans, please explain it to me :-).
Any comments are welcome.
Thanks and reply to me directly if possible,
Dmitry Antipov
Dmitry wrote:
I'm new to Python but quite familiar with Lisp. So my question is about Python list-related functions. Why append(), extend(), sort(), reverse() etc. doesn't return a reference to it's own (modified) argument ?
doesn't Lisp have a FAQ? ;-) http://www.python.org/doc/FAQ.html#6.20 Q. Why doesn't list.sort() return the sorted list? ... basically, operations that modify an object generally don't return the object itself, to avoid mistakes like: for item in list.reverse(): print item # backwards ... for item in list.reverse(): print item # backwards, or? a slightly more pythonic way would be to add sorted, extended, reversed (etc) -- but that leads to method bloat. in addition, based on studying huge amounts of python code, I doubt cascading list operations would save the world that much typing... followups to python-list@python.org </F>
participants (2)
-
dmitry.antipovīŧ auriga.ru
-
Fredrik Lundh