Inconsistency in Python's Comparisons

Dan Bishop danb_83 at yahoo.com
Wed Mar 17 02:39:51 EST 2004


Erik Max Francis <max at alcyone.com> wrote in message news:<4057B2A4.F9EC49DD at alcyone.com>...
> Dietrich Epp wrote:
> 
> > This could cause sorting functions to malfunction.
> > [example of nontransitive "<"]
>
> Do you have an example of this that doesn't involve comparisons between
> different types?

# This shouldn't count, for obvious reasons.  But what the heck...

class ContrivedExample(object):
   def __init__(self, n):
      self.__n = n
   def __cmp__(self, other):
      if self.__n == other.__n:
         return 0
      elif (self.__n - other.__n) % 3 == 1:
         return -1
      else:
         return 1

paper = ContrivedExample(0)
rock = ContrivedExample(1)
scissors = ContrivedExample(2)

print paper < scissors
print scissors < rock
print rock < paper



More information about the Python-list mailing list