class T(str): def __matmul__(self, xs): return self[::-1] + xs[::-1] def __rmatmul__(self, xs): return xs[::-1] + self[::-1] def __imatmul__(self, xs): return self[::-1] + xs[::-1] xs = T('ahoj') ys = T('123') # __matmul__ print(xs @ ys) # 'joha321' print(xs @ 'ABC') # 'johaCBA' # __rmatmul__ print('ABC' @ xs) # bez __rmatmul__: # TypeError: unsupported operand type(s) for @: 'str' and 'T' # s __rmatmul__: # 'CBAjoha' # __imatmul__ zs = T('!?') zs @= ys print(zs) # '?!321'