There's probably a duplicate for this somewhere, but I can't think of it.
Here's how you'd do it -- you need to use the magic methods that those operators are shortcuts for!
def maths(operator):
mapping = { "+": final.__add__,
"-": final.__sub__,
"x": final.__mul__,
"*": final.__mul__,
"/": final.__truediv__,
"//": final.__floordiv__}
return mapping[operator](number)
It's a big complicated, but basically the way those operators work under the hood is by calling a magic method as described here. You could make a "faux number" by doing:
class FakeNumber(object):
def __init__(self, value=0):
self.value = value
def __add__(self,other):
return self.value + other
def __sub__(self,other):
return self.value - other
def __mul__(self,other):
return self.value * other
def __truediv__(self,other):
return self.value / other
def __floordir__(self,other):
return self.value // other
Now that all your magic methods are implemented, you can do:
two = FakeNumber(2)
two * 4 # 8
two + 2 # 4
two / 2 # 1
two - 2 # 0