Next / Previous / Contents / Shipman's homepage

8.6. OldInfo.__cmp__(): The comparator method

This method defines how comparison operators work when comparing two OldInfo objects. The returned value uses the same conventions as Python's built-in cmp() function: it returns a negative number if the first argument should come first, a positive number if the second argument should come first, and zero if they are considered equal.

To get this effect, we can simply call the cmp() function and pass it the two timestamps. The result of that function would order the objects in chronological order, so we could negate that result to get reverse chronological order. Or, as is done here, we could just reverse the order of the arguments.

oldfiles.py
# - - -   O l d I n f o . _ _ c m p _ _   - - -

    def __cmp__ ( self, other ):
        """Compare two OldInfo objects.

          [ other is an OldInfo object ->
              if self should precede other ->
                return a negative number
              else if self should follow other ->
                return a positive number
              else -> return 0 ]
        """
        return  cmp ( other.modEpoch, self.modEpoch )