class myInt(int): def __lt__(self, y): if(isinstance(y, int)): return int.__lt__(self, y) else: self.chain = ('addThenDouble', 1) return self def __add__(self, y): if(isinstance(y, int)): return int.__add__(self, y) else: if(self.chain and self.chain == ('addThenDouble', 1)): self.chain = ('addThenDouble', 2) return self else: delattr(self, 'chain') raise Exception('Invalid Syntax') def __gt__(self, y): if('chain' in self.__dict__ and self.chain == ('addThenDouble', 2)): delattr(self, 'chain') return (self + y) * 2 else: return int.__gt__(self, y) # Goal: Define x <+> y as (x + y) * 2 # Operators work in normal situations print(myInt(3) < myInt(4), myInt(3) < myInt(3)) # True False print(myInt(3) + myInt(4), myInt(myInt(4) + myInt(4))) # 7 8 print(myInt(3) > myInt(4), myInt(3) > myInt(3)) # True False print(((myInt(3) <())+())> 4) # (x + y) * 2 = 14