copysort patch, was RE: [Python-Dev] inline sort option

Alex Martelli aleaxit at
Tue Oct 21 07:55:02 EDT 2003

On Tuesday 21 October 2003 01:18 pm, Mark Russell wrote:
> On Tue, 2003-10-21 at 12:00, Alex Martelli wrote:
> > Why should multiple data types acquire separate .sort methods with
> > subtly different semantics (one works in-place and returns None, one
> > doesn't mutate the object and returns a list, ...) when there's no real
> > added value wrt ONE classmethod of list...?
> I agree that the different semantics for lists and dicts are a strike
> against this.  The argument for it is that walking over a dictionary in
> sorted order is (at least to me) a missing idiom in python.  Does this

It's a frequently used idiom (actually more than one) -- it's not

> never come up when you're teaching the language?

Sure, and I have a good time explaining that half the time you
want to sort on KEYS and half the time on VALUES.  An example
I often use is building and displaying a word-frequency index: now
it's pretty obvious that you may want to display it just as easily
by frequency (most frequent words first) OR alphabetically.

The key= construct IS a huge win, btw.  I just wish there WAS an
easier way to express the TYPICAL keys one wants to use than
    lambda x: x[N]
for some N or
    lambda x: x.A
for some A.  getattr and operator.getitem are no use, alas, even
when curried, because they take x first:-(.  I'd rather not teach lambda
(at least surely not early on!) so I'll end up with lots of little def's
(whose need had sharply decreased with list comprehensions, as
map and filter moved into a corner to gather dust).  Ah well.

> I wouldn't advocate adding this to other types (e.g. Set) because
> they're much less commonly used than dicts, so I don't think there's a

Actually, I was thinking of presenting them BEFORE dicts next time
I have an opportunity of teaching Python from scratch.  The ARE
simpler and more fundamental, after all.

> danger of a creeping plague of sort methods.  Not a big deal though -
> list.sorted() is the real win.

I concur.

> Mark Russell
> PS: I'm really not an anal-retentive keystoke counter :-)

OK, sorry for the digs, it just _looked_ that way for a sec;-).


More information about the Python-Dev mailing list