[Tutor] cmp()

Rob rob@uselesspython.com
Wed, 14 Aug 2002 16:46:24 -0500


This is from the Python documentation:

"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."

932 is greater than 22, so '1' is returned.
"abc" and "abc" are equal, so '0' is returned.
"abc" is less than "def", so '-1' is returned.

If I did my job right, it becomes more clear as Joe Useless fleshes out his
program during the course of the article. He develops it so that it provides
more meaningful output than '-1', '0', and '1', precisely because there is
room for confusion. cmp() is only one of many tools available to the
programmer that might benefit from more meaningful output (at least from the
end user perspective). Of course, if another function is calling cmp(), it
probably wants to see the numerical output.

3;->
Rob
http://uselesspython.com

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> Kyle Babich
> Sent: Wednesday, August 14, 2002 4:25 PM
> To: tutor
> Subject: [Tutor] cmp()
>
>
> I was just reading the UselessPython tutorial with the cmp() program.
> I haven't learned cmp() yet so while I was reading it I thought I would
> launch IDLE and try it out, but I can't make sense of it.
>
> >>> cmp(937, 22)
> 1
> >>> cmp("abc", "abc")
> 0
> >>> cmp("abc", "def")
> -1
>
> Why does it say 937 and 22 are equal?  Why does it say abc and abc
> aren't equal?  Why does it say -1 for abc and def, shouldn't it say 0?
> How do I make sense of this?
>
> Thank you,
> --
> Kyle
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>