Help, how to override <= operator

Clemens Hintze cle at gmx.net
Wed May 19 12:59:37 EDT 1999


jk94r at ecs.soton.ac.uk (Joseph Kuan) writes:

>	What's the function name for overriding the <= operator?

AFAIK, nothing! ;-)

You have to implement the method __cmp__ in your class. That method
should have two parameters, self and the second object. After
comparsion it should return -1, 0, 1 depending, whether self was 
less, equal, greater than the second object.

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

HTH,
Clemens.

[...]




More information about the Python-list mailing list