I'm trying to make a "rock, paper, scissors" game in Python and i want to set one variable to be greater then the other one.
paper > rock scissors > paper rock > scissors
how could i do it..?
Yes as Daniel said, with the override of
__cmp__ you can acheive this :
class Comparable(object): def __init__(self, below_class, above_class): self.above_class = above_class self.below_class = below_class def __cmp__(self, other): if isinstance(other, self.below_class): return 1 if isinstance(other, self.above_class): return -1 return 0 class Paper(Comparable): pass class Rock(Comparable): pass class Scissors(Comparable): pass scissors = Scissors(Paper, Rock) paper = Paper(Rock, Scissors) rock = Rock(Scissors, Paper) # True print paper > rock print scissors > paper print rock > scissors # False print paper < rock print scissors < paper print rock < scissors
The documentation on the way this works is available here : https://docs.python.org/2.6/reference/datamodel.html#object.cmp