Fast attribute/list item extraction

Raymond Hettinger vze4rx4y at verizon.net
Tue Dec 2 03:19:25 EST 2003


> > Please can we have a name that better expresses the functionality?
> >
> > E.g., 'keyfunc', or 'xform' or something that says the method is expecting
> > a function that will be called with the sorting argument to produce the
value
> > used in sorting? "key=" doesn't do it IMO.
>
> I agree. To me, the word "key" has deep meaning with respect to hash tables
> and databases, and reading it as a parameter makes me first think it might
> require a string or integer. I would prefer something like "sortfunc" or
> "decorator", but both of your suggestions would be fine by me and preferable
> to "key".

cmpfunc= and keyfunc= were both suggested by several non-Dutch such as myself.

Then, Guido rightly pointed out that the keywords should just be pithy memory
joggers. Taken in that light, key= and cmp= do the job nicely.  The definitions
and details are best left to docstrings and regular docs.  Besides, after
learning the new DSU key just once, we *know* what kind of function is supposed
to go there. Further, when reading code, you get the additional context of
seeing which function was actually used:

   logentries.sort(key=lambda e: e.juliandate)      # looks like a key function
to me


signature_line(fond_adieu=True, semi_serious=True, has_a_point=True,
message_body_text_adverb_stem="Sincere")-ly yours,


Raymond Hettinger






More information about the Python-list mailing list