Opened 6 years ago

Closed 3 years ago

#16536 closed enhancement (wontfix)

Python 3 preparation: cmp parameter of sorted() function and L.sort() method are gone

Reported by: wluebbe Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: python3 Keywords: python3
Cc: tscrim, embray, jdemeyer Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #16853, #17200, #17229 Stopgaps:

Description

In Py2 the standard function sorted() and the mutable list method sort() both have an optional parameter cmp= that takes a custom comparison function of two arguments.

The parameter is no more available in Py3.

The recommended way is to use the key= which specifies a function of one argument that is used to extract a comparison key from each list element. This has also better performance.

While there is a function functools.cmp_to_key() to convert an old-style cmp function to a key function, it is preferable to directly use an appropriate key function.

This ticket is tracked as a dependency of meta-ticket ticket:15980.

Attachments (1)

exercise-cmp-key-parameters.py (4.1 KB) - added by wluebbe 6 years ago.
Demonstrate behavior of cmp= and key= in Py2 and Py3

Download all attachments as: .zip

Change History (17)

Changed 6 years ago by wluebbe

Demonstrate behavior of cmp= and key= in Py2 and Py3

comment:1 Changed 6 years ago by wluebbe

The workings of functools.cmp_to_key is nicely explained in stackoverflow.

This is also demonstrated in the attached script.

It is also shown that it is not a good idea the use "cmp=" and "key=" together even in Py2!

comment:2 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:3 Changed 6 years ago by aapitzsch

  • Dependencies set to #16853

comment:4 Changed 6 years ago by aapitzsch

  • Dependencies changed from #16853 to #16853, #17200

comment:5 Changed 6 years ago by aapitzsch

  • Dependencies changed from #16853, #17200 to #16853, #17200, #17229

comment:6 Changed 4 years ago by jdemeyer

  • Component changed from distribution to python3

comment:7 Changed 4 years ago by chapoton

see #21035 for a step in that direction

comment:8 Changed 4 years ago by chapoton

  • Milestone changed from sage-6.4 to sage-7.4
  • Type changed from PLEASE CHANGE to enhancement

comment:9 Changed 4 years ago by chapoton

This is now mostly done. In some places, cmp was only deprecated.

comment:10 Changed 4 years ago by chapoton

  • Milestone changed from sage-7.4 to sage-7.6

comment:11 Changed 3 years ago by chapoton

  • Milestone changed from sage-7.6 to sage-8.0

comment:12 Changed 3 years ago by chapoton

another step in #23698

comment:13 Changed 3 years ago by chapoton

One should soon take care of removing what was deprecated in #21766.

See #24409 for that

Last edited 3 years ago by chapoton (previous) (diff)

comment:14 Changed 3 years ago by chapoton

  • Milestone changed from sage-8.0 to sage-duplicate/invalid/wontfix
  • Status changed from new to needs_review

Maybe we could now close this one ?

comment:15 Changed 3 years ago by chapoton

  • Cc tscrim embray jdemeyer added

ping ? any objection to close as invalid ?

comment:16 Changed 3 years ago by embray

  • Resolution set to wontfix
  • Status changed from needs_review to closed
Note: See TracTickets for help on using tickets.