MCPcopy Create free account
hub / github.com/dabeaz-course/python-mastery / MutInt

Class MutInt

Solutions/2_4/mutint.py:8–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7@total_ordering
8class MutInt:
9 __slots__ = ['value']
10
11 def __init__(self, value):
12 self.value = value
13
14 def __str__(self):
15 return str(self.value)
16
17 def __repr__(self):
18 return f'MutInt({self.value!r})'
19
20 def __format__(self, fmt):
21 return format(self.value, fmt)
22
23 # Implement the "+" operator. Forward operands (MutInt + other)
24 def __add__(self, other):
25 if isinstance(other, MutInt):
26 return MutInt(self.value + other.value)
27 elif isinstance(other, int):
28 return MutInt(self.value + other)
29 else:
30 return NotImplemented
31
32 # Support for reversed operands (other + MutInt)
33 __radd__ = __add__
34
35 # Support for in-place update (MutInt += other)
36 def __iadd__(self, other):
37 if isinstance(other, MutInt):
38 self.value += other.value
39 return self
40 elif isinstance(other, int):
41 self.value += other
42 return self
43 else:
44 return NotImplemented
45
46 # Support for equality testing
47 def __eq__(self, other):
48 if isinstance(other, MutInt):
49 return self.value == other.value
50 elif isinstance(other, int):
51 return self.value == other
52 else:
53 return NotImplemented
54
55 # One relation is needed for @total_ordering decorator. It fills in others
56 def __lt__(self, other):
57 if isinstance(other, MutInt):
58 return self.value < other.value
59 elif isinstance(other, int):
60 return self.value < other
61 else:
62 return NotImplemented
63
64 # Conversions to int() and float()
65 def __int__(self):

Callers 1

__add__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected