__cmp__ method

J

JH

Hi

Can anyone explain to me why the following codes do not work? I want to
try out using __cmp__ method to change the sorting order. I subclass
the str and override the __cmp__ method so the strings can be sorted by
the lengh. I expect the shortest string should be in the front. Thanks
def __init__(self, s):
str.__init__(self, s) # Ensure super class is initialized
def __cmp__(self, other):
return cmp(len(self), len(other))
a = myStr('abc')
b = myStr('Personal')
c = myStr('Personal firewall')
sorted([c, b, a]) ['Personal', 'Personal firewall', 'abc']
 
M

marek.rocki

Python documentation says:
__cmp__( self, other)
Called by comparison operations if rich comparison (see above) is not defined.
So it seems you have to redefine rich comparisons __lt__, __gt__,
__eq__ etc as well.

If all you need is change sorting order, why not use appropriate
parameters of sorted() function (cmp=... or key=...)?
 
J

Jon Clements

This probably isn't exactly what you want, but, unless you wanted to do
something especially with your own string class, I would just pass a
function to the sorted algorithm.

eg:

sorted( [a,b,c], cmp=lambda a,b: cmp(len(a),len(b)) )

gives you the below in the right order...

Never tried doing what you're doing, but something about builtin types,
and there's a UserString module...

Hope that helps a bit anyway,

Jon.
Hi

Can anyone explain to me why the following codes do not work? I want to
try out using __cmp__ method to change the sorting order. I subclass
the str and override the __cmp__ method so the strings can be sorted by
the lengh. I expect the shortest string should be in the front. Thanks
def __init__(self, s):
str.__init__(self, s) # Ensure super class is initialized
def __cmp__(self, other):
return cmp(len(self), len(other))
a = myStr('abc')
b = myStr('Personal')
c = myStr('Personal firewall')
sorted([c, b, a]) ['Personal', 'Personal firewall', 'abc']
 
G

George Sakkis

Jon said:
This probably isn't exactly what you want, but, unless you wanted to do
something especially with your own string class, I would just pass a
function to the sorted algorithm.

eg:

sorted( [a,b,c], cmp=lambda a,b: cmp(len(a),len(b)) )

gives you the below in the right order...

Or even better in 2.4 or later:
sorted([a,b,c], key=len)

George
 
B

bruno at modulix

JH said:
Hi

Can anyone explain to me why the following codes do not work? I want to
try out using __cmp__ method to change the sorting order. I subclass
the str and override the __cmp__ method so the strings can be sorted by
the lengh. I expect the shortest string should be in the front. Thanks

AFAIK (please a Guru correct me if I'm wrong), __cmp__ is used as a
fallback when other comparison operators ( __lt__ etc) are not implemented :
.... def __lt__(self, other):
.... return len(self) < len(other)
.... def __le__(self, other):
.... return len(self) <= len(other)
.... def __gt__(self, other):
.... return len(self) > len(other)
.... def __ge__(self, other):
.... return len(self) >= len(other)
....
items = [MyStr("lolo lala"), MyStr("lolo"), MyStr("alal"), MyStr("a")]
sorted(items) ['a', 'lolo', 'alal', 'lolo lala']

But anyway, all this is a WTF. Just using the correct params for sorted
is enough:
items2 = ['lolo lala', 'lolo', 'alal', 'a']
sorted(items2, key=len)
['a', 'lolo', 'alal', 'lolo lala']

def __init__(self, s):
str.__init__(self, s) # Ensure super class is initialized

This is useless. If you don't override it, parent's init will be called
anyway.

(snip)

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top