We lost cmp in python3

This is because of PEP207. They proposed rich comparison; They are <, >, <=, >=, ==, !=, which associates to lt, gt, le, ge, eq, and ne. cmp is no longer used in it.

Now when you want to pass a comparison to sort you might have to define your own.
The 2.7 handbook says:

cmp(x, y)

Compare the two objects x and y and return an integer according to the outcome. The return value is negative if x < y, zero if x == y and strictly positive if x > y.

So we can write:

def my_cmp(x,y):
    (a > b) - (a < b)

This is actually the official work around in the documentation.FOR REAL
It will return 1 if a > b, -1 if a < b and 0 if a == b

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax