Help, how to override <= operator
Clemens Hintze
cle at qiao.in-berlin.de
Thu May 20 14:47:46 EDT 1999
Frank Niessink <frankn=news at cs.vu.nl> writes:
>Clemens Hintze <cle at gmx.net> wrote:
>> Something like that:
[...]
>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.
Ahem, oh, uh! You're right, of course. I ever initialize instance
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'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 :-)
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 :-)
But I have thought, that it was not very clear, if I show, that
"__cmp__" has to use "cmp" to do its work :-) Perhaps I was wrong
here!
But now, thanks of your friendly correction, he can see, how a profi
would do it the right way :-)))
>Cheers, Frank
Ciao, Clemens
[...]
More information about the Python-list
mailing list