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

SourceForge.net noreply at sourceforge.net
Mon Aug 29 17:35:21 CEST 2005


Bugs item #1275719, was opened at 2005-08-29 16:54
Message generated for change (Comment added) made by pitrou
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: Open
Resolution: None
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: Antoine Pitrou (pitrou)
Date: 2005-08-29 17: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 17: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