forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_inplace.py
More file actions
109 lines (82 loc) · 1.73 KB
/
operator_inplace.py
File metadata and controls
109 lines (82 loc) · 1.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class InPlace:
def __init__(self, val):
self.val = val
def __ipow__(self, other):
self.val **= other
return self
def __imul__(self, other):
self.val *= other
return self
def __imatmul__(self, other):
# I guess you could think of an int as a 1x1 matrix
self.val *= other
return self
def __itruediv__(self, other):
self.val /= other
return self
def __ifloordiv__(self, other):
self.val //= other
return self
def __imod__(self, other):
self.val %= other
return self
def __iadd__(self, other):
self.val += other
return self
def __isub__(self, other):
self.val -= other
return self
def __ilshift__(self, other):
self.val <<= other
return self
def __irshift__(self, other):
self.val >>= other
return self
def __iand__(self, other):
self.val &= other
return self
def __ixor__(self, other):
self.val ^= other
return self
def __ior__(self, other):
self.val |= other
return self
i = InPlace(2)
i **= 3
assert i.val == 8
i = InPlace(2)
i *= 2
assert i.val == 4
i = InPlace(2)
i @= 2
assert i.val == 4
i = InPlace(1)
i /= 2
assert i.val == 0.5
i = InPlace(1)
i //= 2
assert i.val == 0
i = InPlace(10)
i %= 3
assert i.val == 1
i = InPlace(1)
i += 1
assert i.val == 2
i = InPlace(2)
i -= 1
assert i.val == 1
i = InPlace(2)
i <<= 3
assert i.val == 16
i = InPlace(16)
i >>= 3
assert i.val == 2
i = InPlace(0b010101)
i &= 0b111000
assert i.val == 0b010000
i = InPlace(0b010101)
i ^= 0b111000
assert i.val == 0b101101
i = InPlace(0b010101)
i |= 0b111000
assert i.val == 0b111101