[ python-Bugs-1275719 ] discrepancy between str.__cmp__ and unicode.__cmp__

SourceForge.net noreply at sourceforge.net
Sat Sep 3 19:01:40 CEST 2005


Bugs item #1275719, was opened at 2005-08-29 09:54
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1275719&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Antoine Pitrou (pitrou)
Assigned to: Nobody/Anonymous (nobody)
Summary: discrepancy between str.__cmp__ and unicode.__cmp__

Initial Comment:
I had the surprise, while wanted to use str.__cmp__ as
the cmp argument to list.sort(), that it seems buggy
compared to unicode.__cmp__, and that these methods
seem implemented quite differently (they have a
different type):

$ python
Python 2.4.1 (#2, Aug 25 2005, 18:20:57)
[GCC 4.0.1 (4.0.1-2mdk for Mandriva Linux release
2006.0)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> unicode.__cmp__
<slot wrapper '__cmp__' of 'unicode' objects>
>>> str.__cmp__
<method-wrapper object at 0xb7a164ac>
>>> u'a'.__cmp__(u'b')
-1
>>> 'a'.__cmp__('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute '__cmp__'
>>> unicode.__cmp__(u'a', u'b')
-1
>>> str.__cmp__('a', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: expected 1 arguments, got 2


Am I missing something ?


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-09-03 12:01

Message:
Logged In: YES 
user_id=80475

In the absence of a defined __cmp__ method for string
objects, str.__cmp__ inherits type.__cmp__ which is used for
comparing types.  That is why you can sort a list of types:


>>> sorted([int, complex, float, str, dict, list, tuple])
[<type 'complex'>, <type 'dict'>, <type 'float'>, <type
'int'>, <type 'list'>, <type 'str'>, <type 'tuple'>]

----------------------------------------------------------------------

Comment By: Antoine Pitrou (pitrou)
Date: 2005-08-29 10:35

Message:
Logged In: YES 
user_id=133955

You are right, I also forgot there is a builtin cmp()
function that works like expected. Still str.__cmp__'s
behaviour is a bit puzzling to me...


----------------------------------------------------------------------

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-08-29 10:16

Message:
Logged In: YES 
user_id=1188172

String comparison is done with rich compare methods, namely
__lt__, __le__, __gt__, __ge__ and __eq__, __ne__.

Why str.__cmp__ exists and 'a'.__cmp__ does not, I cannot say.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1275719&group_id=5470


More information about the Python-bugs-list mailing list