Python - __cmp__ Method

Introduction

In Python 2.X only, the __cmp__ method is used as a fallback if more specific methods are not defined.

Its integer result is used to evaluate the operator being run.

The following produces the same result as the prior section's code under 2.X, for example, but fails in 3.X because __cmp__ is no longer used:

class C: 
    data = 'test'                          # 2.X only 
    def __cmp__(self, other):              # __cmp__ not used in 3.X 
        return cmp(self.data, other)       # cmp not defined in 3.X 

X = C() 
print(X > 'ham')                           # True  (runs __cmp__) 
print(X < 'ham')                           # False (runs __cmp__)