473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Trouble with max() and __cmp__()

My code:

class Policy(list):
def __cmp__(self,other):
return cmp(self.fitness,other.fitness)

j = Policy()
j.fitness = 3
k = Policy()
k.fitness = 1
l = Policy()
l.fitness = 5
print max([j,k,l]).fitness

prints 3, when I was expecting it to print 5. What have I done wrong?

Thanks for the help,
THN

Jan 28 '07 #1
4 1176
Thomas Nelson wrote:
My code:

class Policy(list):
def __cmp__(self,other):
return cmp(self.fitness,other.fitness)
Define method __gt__.
Jan 28 '07 #2


On Jan 28, 3:13Â*pm, Wojciech MuÅ‚a
<wojciech_m...@poczta.null.onet.pl.invalidwrote:
>Define method __gt__.
This works, thanks. I was a little surprised though. is __cmp__ used
by any builtin functions?

Thanks,
THN

Jan 28 '07 #3
Thomas Nelson schrieb:
On Jan 28, 3:13 pm, Wojciech Muła
<wojciech_m...@poczta.null.onet.pl.invalidwrote:
>Define method __gt__.

This works, thanks. I was a little surprised though. is __cmp__ used
by any builtin functions?
It is used by max() if the object doesn't implement __gt__.

Regards,
Martin
Jan 28 '07 #4
At Sunday 28/1/2007 18:21, Thomas Nelson wrote:
><wojciech_m...@poczta.null.onet.pl.invalidwrote :
Define method __gt__.

This works, thanks. I was a little surprised though. is __cmp__ used
by any builtin functions?
The problem is, rich comparison functions take precedence over
__cmp__, so if your base class (list in this case) already defines
rich comparison, you have to redefine them all.
For simple cases, you can just redefine __cmp__ and reimplement all
others __gt__, __lt__ etc using it.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
10
by: gregory_may | last post by:
I have an application I created called "JpegViewer.exe". It simply loads a Jpeg file and displays in on the screen. It works great, in my lab. When I am using it at a customer site, things...
39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
2
by: apec | last post by:
Dear All, I'm having trouble getting my c++ code to work (below). Any suggestions as to what could be the problem? I think it has to do with the while loop, since the program ends before command...
1
by: pintoamanda | last post by:
Hello, I'm trying to have two actions work when the mouse is clicked on a button. It's supposed to do both a "Set Image URL" and a "Go To Link" simultaneously. It indeed does this. However, if...
26
by: Ping | last post by:
Hi, I'm wondering if it is useful to extend the count() method of a list to accept a callable object? What it does should be quite intuitive: count the number of items that the callable returns...
6
by: xkenneth | last post by:
Looking to do something similair. I'm working with alot of timestamps and if they're within a couple seconds I need them to be indexed and removed from a list. Is there any possible way to index...
19
by: Eugeny Myunster | last post by:
I know, only simple one: #include <stdio.h> int main() { int min=0,max=0,i,arr; for(i=0;i<12;i++) arr=rand()%31-10; for(i=0;i<12;i++)
9
by: George Sakkis | last post by:
I want to sort sequences of strings lexicographically but those with longer prefix should come earlier, e.g. for s = , the sorted sequence is . Currently I do it with: s.sort(cmp=lambda x,y: 0 if...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.