Basically my Question: Is there an elegant way, if I have a given Container class
class Container:
def __init__(self, value):
self.value = value
to pass any operator to the value parameter of the Container class?
So one obvious solution would be to override any single operator on its own. So for example for + I could do:
class Container:
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.value + other
so that for example
c1 = Container(1)
c1 += 1
print(c1)
would result in
2
and
c2 = Container("ab")
c2 += "c"
print(c2)
would result in
abc
So what I could do is to override all arithmetic built-in functions from python. But my Question is, is there a more elegant (shorter) way to do this for any operator?