Help, how to override <= operator

Frank Niessink frankn=news at cs.vu.nl
Fri May 21 13:34:07 EDT 1999


Clemens Hintze <cle at qiao.in-berlin.de> wrote:

> Ahem, oh, uh! You're right, of course. I ever initialize instance
                                          ^^^^^ did you mean: never?
> variables as class variables first in Python. I don't know why, but I
> use "__init__" very seldom to initialize them to a fixed value (like
> None). Only if they are initialized via parameters of "__init__". 

> Later on, if the program is running I delete the unnecessary ones,
> sometimes :-}

I'm a bit confused about what you here. 

>>... but I guess your implementation of __cmp__ was done that way 
>>for educational purposes :-)

> You're right in that one! It was really only for educational purpose.
> Normally I also would use "cmp". Especially, as "<", ">" and "==" in
> my example also would use "cmp" at last :-)

Understood. 

> But now, thanks of your friendly correction, he can see, how a profi
> would do it the right way :-)))

Me profi? Not really. Here's the implementation Arcege (Michael Reilly)
send me, which is even better (he said: for sharing purposes, so I 
guess he doesn't mind me repeating it here):

class Myint:
	def __init__(self, n = 0):
		self.i = n
	def __cmp__(self, other):
		return cmp(self.i, other)
	def __rcmp__(self, other): 
		return cmp(other, self.i)             

which allows you to compare your own integers with standard integers and 
floats and longs...

>>> i = Myint()
>>> 0L <= i < 0.3
1


Pretty cool eh?

Cheers, Frank

-- 
Important facts from Galactic history, number one: (Reproduced from the 
Siderial Daily Mentioner's Book of popular Galactic History.) The night sky
over the planet Krikkit is the least interesting sight in the entire Universe.
	-- Douglas Adams, 'Life, the Universe, and Everything'




More information about the Python-list mailing list