# Simple calculator class

#1

Albeit perhaps a bit of silliness, as an exploration it was (and remains) a bit of fun, too…

``````class Number:
def __init__(self, value):
self.value = value
def plus(self, b):
return self.value + b
def minus(self, b):
return self.value - b
def times(self, b):
return self.value * b
def divide_by(self, b):
return self.value / b
def floor_divide_by(self, b):
return self.value // b
def mod(self, b):
return self.value % b
def raise_to(self, b):
return self.value ** b
def compute(self, b, f):
return {
'+': self.plus(b),
'-': self.minus(b),
'*': self.times(b),
'/': self.divide_by(b),
'//': self.floor_divide_by(b),
'%': self.mod(b),
'**': self.raise_to(b)
}[f]
``````

This gives us a number class with built in operations.

``````>>> a = Number(42)
>>> b = Number(a.compute(21, '+'))
>>> b.value
63
>>> b.compute(7,'/')
9.0
>>> b.compute(9,'//')
7
>>> b.compute(a.value, '/')
1.5
>>>
``````

Once this is wired to a user interface, button presses can be translated to strings and the lookup table converts them to method calls.