-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_calculator.py
56 lines (46 loc) · 1.39 KB
/
simple_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Calculator:
def __init__(self, init_value=0):
self.value = init_value
def __add__(self, args):
if isinstance(args, Calculator):
self.value += args.value
return self
else:
self.value += args
return self
def __setattr__(self, key, value):
if len(self.__dict__) < 12:
self.__dict__[key] = value
def __sub__(self, args):
if isinstance(args, Calculator):
self.value -= args.value
return self
else:
self.value -= args
return self
def __mul__(self, args):
if isinstance(args, Calculator):
self.value *= args.value
return self
else:
self.value *= args
return self
def __truediv__(self, args):
if isinstance(args, Calculator):
self.value /= args.value
return self
else:
self.value /= args
return self
def __pow__(self, power, modulo=None):
if isinstance(power, Calculator):
self.value **= power
return self
else:
self.value **= power
return self
def __str__(self):
return str(self.__dict__)
def __iter__(self):
return self.value
print((Calculator(2) + Calculator(5) - Calculator(6)) * Calculator(3) ** 2 / Calculator(3))