This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients gvanrossum, rhettinger
Date 2008-01-14.23:24:36
SpamBayes Score 0.013681869
Marked as misclassified No
Message-id <1200353078.98.0.0523145585117.issue1771@psf.upfronthosting.co.za>
In-reply-to
Content
Yes, it does feel great.  The code is cleaner and faster.  The API is
simple and matches all the other key= functions in
min/max/nsmallest/nlargest/groupby.

After more thought, I would like to make one more change and require the
arguments to be keywords such as sort(key=str.lower) but not
sort(str.lower).

The issue is that the cmp= interface has been around so long that it is
ingrained into our thinking and in our code.  Having to write-out the
keyword makes the intent explicit and will avoid accidently passing in a
cmp= function when a key= function was intended.  In Py3.1, the
restriction could be relaxed and l.sort(f) could be accepted for
l.sort(key=f).

For the 2-to-3 tool, I wrote a converter that automatically transitions
code currently using a custom compare function:

2.6 code:  s.sort(cmp=lambda p, q: cmp(p.lower(), q.lower()))
3.0 code:  s.sort(key=CmpToKey(lambda p, q: cmp(p.lower(), q.lower())))

Ideally, the automatcic conversion would be accompanied by a suggestion
to manually rewrite to something like:

3.0 code:   s.sort(key=str.lower)

--- converter code ---

def CmpToKey(mycmp):
    'Convert a cmp= function into a key= function'
    class K(object):
        def __init__(self, obj, *args):
            self.obj = obj
        def __cmp__(self, other):
            return mycmp(self.obj, other.obj)
    return K
History
Date User Action Args
2008-01-14 23:24:39rhettingersetspambayes_score: 0.0136819 -> 0.013681869
recipients: + rhettinger, gvanrossum
2008-01-14 23:24:39rhettingersetspambayes_score: 0.0136819 -> 0.0136819
messageid: <1200353078.98.0.0523145585117.issue1771@psf.upfronthosting.co.za>
2008-01-14 23:24:37rhettingerlinkissue1771 messages
2008-01-14 23:24:36rhettingercreate