A small suggestion for Python
Fredrik Lundh
fredrik at effbot.org
Sat Jan 13 06:48:57 EST 2001
from the when-did-you-last-read-the-faq-before-posting department
(or maybe the of-course-guido's-already-thought-of-that department):
> > I have been programming in Python for several years. I still
> > occasionally make mistakes with functions (such as sort) that
> > return only "None". I suggest that functions like sort should
> > return either "Nothing" or nothing. "Nothing" would be exactly
> > like "None" except it can't be assigned. Whether "Nothing" or
> > nothing is used, the statement "x= y.sort()" would raise a
> > run-time Exception.
today, chances are that you get a run-time exception when
you try to do something with "x". That's of course the reason
sort returns None and not the sorted list (see below).
(sure, if you don't do something with x, you won't get an exception --
but in that case, does it really matter?)
> Another idea: It could return what we all excpect to be returned: The object
> that was sorted! So if a list is sorted, why not return the sorted list?
http://www.python.org/doc/FAQ.html#6.20
6.20. Why doesn't list.sort() return the sorted list?
"In situations where performance matters, making a copy of the
list just to sort it would be wasteful. Therefore, list.sort() sorts
the list in place. In order to remind you of that fact, it does not
return the sorted list. This way, you won't be fooled into
accidentally overwriting a list when you need a sorted copy
but also need to keep the unsorted version around."
Cheers /F
More information about the Python-list
mailing list