Next / Previous / Contents / Shipman's homepage

6.17. PathInfo.__cmp__(): Define the comparison operator on PathInfo objects

We want PathInfo objects to sort by their .path attributes, that is, in ascending order by pathname.

Because these pathnames are simple strings, and because Python's built-in cmp() function can compare strings, all we have to do here is call cmp() on the pathnames and return its result as our result.

pathinfo.py
# - - -   P a t h I n f o . _ _ c m p _ _   - - -

    def __cmp__ ( self, other ):
        """Comparison operator for PathInfo objects.

          [ other is a PathInfo object ->
              if self.path < other.path ->
                return a negative number
              else if self.path > other.path ->
                return a positive number
              else ->
                return 0 ]
        """
        return cmp ( self.path, other.path )