A small suggestion for Python

Tim Peters tim.one at home.com
Sat Jan 13 15:13:29 EST 2001


[Franz GEIGER]
> Another idea: It could return what we all excpect to be returned:
> The object that was sorted!

As a general rule, in Python, methods that alter a mutable object in-place
return None, not the object.  list.sort() is only one such.  list.insert()
and list.append() and list.extend() and list.reverse() and list.remove() and
dict.update() and dict.clear() and ... are others.

> So if a list is sorted, why not return the sorted list?

Because Guido doesn't want to <0.9 wink>.  Python has been consistent along
a a different axis for 10 years (see above).  Returning the sorted list
wouldn't help, either:  you might stop complaining, but we'd get an at least
equal number of new complaints about, e.g.,

>>> y = x.sort()
>>> y.append(42)

WHY DID THAT CHANGE WHATS IN X???  I DIDN'T CHANGE X!  I CHANGED Y AS ANY
IDIUT CAN SEE.  STUPID LANGWAGE IN PURL THE SORT FUCTION DOSNT DO THAT!!!
HOW CAN ANYONRE USE THE PHYTON WHEN ITS SO STUPID.

(BTW, that's what all complaints look like to Guido <wink>)

In a world where different people want different things at different times,
we can't stop complaints -- we can only choose *which* complaints we want to
endure.  Your complaint is easy to endure, because it's easily addressed
*by* you:

def sort(list):
    list.sort()
    return list

Use that instead, if you're not prone to making the aliasing mistakes that
leads to.  Or, if you are, change your little sort function to make a copy
before sorting, and live with the extra memory burden.  It's your choice to
make, and Guido helps lead you to that realization by making the return
value from mutableobject.mutator() maximally useless <0.7 wink>.

most-improvements-aren't-ly y'rs  - tim





More information about the Python-list mailing list