about sort and dictionary

bonono at gmail.com bonono at gmail.com
Tue Nov 22 18:03:29 EST 2005


Steven D'Aprano wrote:
> There are four possibilities for a construction like list.sort():
>
> (1) sort the list in place and return a reference to the same list;
> (2) sort the list in place and return a copy of the same list;
> (3) sort the list in place and return None;
> (4) don't sort in place and return a sorted list.
>
> No solution is always right, no solution is always wrong, but the most
> flexible is a combination of (3) and (4). Python now has that with sort()
> and sorted(). Prior to the addition of sorted() to the language, (3) was
> considered the best solution because of a simple Python principle: never
> duplicate objects unless explicitly told to.
>
I don't see the reason that (3) and (4) are the most flexible.

Again, "never duplicate objects unless explicitly told to" combined
with "=" is name binding gives me a very strong message that
list.sort() it will change things in place and which is why it is quite
natural(for me at least)

3
2
1
1
2
3

for this language. Wether it is "best" or make more sense doesn't
really matter to me, though I am curious to know why.

But basically, I just use the language as it is, and the way I want to.
So long it solves my problem and gives me the result I want.




More information about the Python-list mailing list