class BetterAddition: def __init__(self, arg): assert arg match arg: case str(): self.strVal = arg case int(): self.intVal = arg case _: print('int or str only') def __add__(self, other): assert other match other: case str(): return f"{(self.strVal if hasattr(self, 'strVal') else str(self.intVal))} {other}" case int(): if hasattr(self, 'intVal'): return other + self.intVal else: return f'{self.strVal} {str(other)}' case _: print('give me a string or int value!') def __repr__(self): return (self.strVal if hasattr(self, 'strVal') else str(self.intVal)) w = BetterAddition w(1) # 1 w('a') # 'a' w(1) + 2 # 3 w(1) + 'b' # '1 b' w('a') + 2 # 'a 2' w('a') + 'b' # 'a b'