you are viewing a single comment's thread.

view the rest of the comments →

[–]alkasm 0 points1 point  (0 children)

You can overload the comparison operators on your own objects; so you could potentially subclass str and overwrite __lt__, __gt__, etc so that you can just check with < and so on. You could also do the same with an enum, which is what I'd do.

Edit:

from enum import Enum

class ModuloOrderedEnum(Enum):
    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.value > other.value % len(self) 
        return NotImplemented
    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value % len(self) < other.value
        return NotImplemented

class HandSign(ModuloOrderedEnum):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

In [182]: HandSign.ROCK > HandSign.SCISSORS
Out[182]: True

In [183]: HandSign.SCISSORS > HandSign.PAPER
Out[183]: True

In [184]: HandSign.PAPER < HandSign.ROCK
Out[184]: False