Help, how to override <= operator

Frank Niessink frankn=news at cs.vu.nl
Thu May 20 09:41:12 EDT 1999


Clemens Hintze <cle at gmx.net> wrote:

> Something like that:

> class myint:
>     i = 0
>     def __init__(self, n):
>         self.i = n
>     def __cmp__(self, b):
>         if self.i < b.i: return -1
>         if self.i > b.i: return 1
>         return 0

This is a bit confusing because you don't need (and don't even use,
and probably don't _want_ to use) the class variable i.

I'd code this as:

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


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

Cheers, Frank
-- 
It wasn't his job to worry about that, though. It was his job to do his job, 
which was to do his job. If that led to a certain narrowness of vision and 
circularity of thought then it wasn't his job to worry about such things.
	-- Douglas Adams, 'Mostly Harmless'




More information about the Python-list mailing list