blob: a37b4e1604c5dfe993fc1ce7cb08e723f407b652 [file] [log] [blame]
Thomas Wouters104a7bc2000-08-24 20:14:10 +00001# Augmented assignment test.
2
3x = 2
4x += 1
5x *= 2
6x **= 2
7x -= 8
8x /= 2
9x %= 12
10x &= 2
11x |= 5
12x ^= 1
13
14print x
15
16x = [2]
17x[0] += 1
18x[0] *= 2
19x[0] **= 2
20x[0] -= 8
21x[0] /= 2
22x[0] %= 12
23x[0] &= 2
24x[0] |= 5
25x[0] ^= 1
26
27print x
28
29x = {0: 2}
30x[0] += 1
31x[0] *= 2
32x[0] **= 2
33x[0] -= 8
34x[0] /= 2
35x[0] %= 12
36x[0] &= 2
37x[0] |= 5
38x[0] ^= 1
39
40print x[0]
41
42x = [1,2]
43x += [3,4]
44x *= 2
45
46print x
47
48x = [1, 2, 3]
49y = x
50x[1:2] *= 2
51y[1:2] += [1]
52
53print x
54print x is y
55
56class aug_test:
Fred Drake004d5e62000-10-23 17:22:08 +000057 def __init__(self, value):
58 self.val = value
59 def __radd__(self, val):
60 return self.val + val
61 def __add__(self, val):
62 return aug_test(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000063
64
65class aug_test2(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000066 def __iadd__(self, val):
67 self.val = self.val + val
68 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +000069
70class aug_test3(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000071 def __iadd__(self, val):
72 return aug_test3(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000073
74x = aug_test(1)
75y = x
76x += 10
77
78print isinstance(x, aug_test)
Fred Drake004d5e62000-10-23 17:22:08 +000079print y is not x
Thomas Wouters104a7bc2000-08-24 20:14:10 +000080print x.val
81
82x = aug_test2(2)
83y = x
84x += 10
85
86print y is x
87print x.val
88
89x = aug_test3(3)
90y = x
91x += 10
92
93print isinstance(x, aug_test3)
94print y is not x
95print x.val
96
97class testall:
98
Fred Drake004d5e62000-10-23 17:22:08 +000099 def __add__(self, val):
100 print "__add__ called"
101 def __radd__(self, val):
102 print "__radd__ called"
103 def __iadd__(self, val):
104 print "__iadd__ called"
105 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000106
Fred Drake004d5e62000-10-23 17:22:08 +0000107 def __sub__(self, val):
108 print "__sub__ called"
109 def __rsub__(self, val):
110 print "__rsub__ called"
111 def __isub__(self, val):
112 print "__isub__ called"
113 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000114
Fred Drake004d5e62000-10-23 17:22:08 +0000115 def __mul__(self, val):
116 print "__mul__ called"
117 def __rmul__(self, val):
118 print "__rmul__ called"
119 def __imul__(self, val):
120 print "__imul__ called"
121 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000122
Fred Drake004d5e62000-10-23 17:22:08 +0000123 def __div__(self, val):
124 print "__div__ called"
125 def __rdiv__(self, val):
126 print "__rdiv__ called"
127 def __idiv__(self, val):
128 print "__idiv__ called"
129 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000130
Fred Drake004d5e62000-10-23 17:22:08 +0000131 def __mod__(self, val):
132 print "__mod__ called"
133 def __rmod__(self, val):
134 print "__rmod__ called"
135 def __imod__(self, val):
136 print "__imod__ called"
137 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000138
Fred Drake004d5e62000-10-23 17:22:08 +0000139 def __pow__(self, val):
140 print "__pow__ called"
141 def __rpow__(self, val):
142 print "__rpow__ called"
143 def __ipow__(self, val):
144 print "__ipow__ called"
145 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000146
Fred Drake004d5e62000-10-23 17:22:08 +0000147 def __or__(self, val):
148 print "__or__ called"
149 def __ror__(self, val):
150 print "__ror__ called"
151 def __ior__(self, val):
152 print "__ior__ called"
153 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000154
Fred Drake004d5e62000-10-23 17:22:08 +0000155 def __and__(self, val):
156 print "__and__ called"
157 def __rand__(self, val):
158 print "__rand__ called"
159 def __iand__(self, val):
160 print "__iand__ called"
161 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000162
Fred Drake004d5e62000-10-23 17:22:08 +0000163 def __xor__(self, val):
164 print "__xor__ called"
165 def __rxor__(self, val):
166 print "__rxor__ called"
167 def __ixor__(self, val):
168 print "__ixor__ called"
169 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000170
Fred Drake004d5e62000-10-23 17:22:08 +0000171 def __rshift__(self, val):
172 print "__rshift__ called"
173 def __rrshift__(self, val):
174 print "__rrshift__ called"
175 def __irshift__(self, val):
176 print "__irshift__ called"
177 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000178
Fred Drake004d5e62000-10-23 17:22:08 +0000179 def __lshift__(self, val):
180 print "__lshift__ called"
181 def __rlshift__(self, val):
182 print "__rlshift__ called"
183 def __ilshift__(self, val):
184 print "__ilshift__ called"
185 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000186
187x = testall()
188x + 1
1891 + x
190x += 1
191
192x - 1
1931 - x
194x -= 1
195
196x * 1
1971 * x
198x *= 1
199
200x / 1
2011 / x
202x /= 1
203
204x % 1
2051 % x
206x %= 1
207
208x ** 1
2091 ** x
210x **= 1
211
212x | 1
2131 | x
214x |= 1
215
216x & 1
2171 & x
218x &= 1
219
220x ^ 1
2211 ^ x
222x ^= 1
223
224x >> 1
2251 >> x
226x >>= 1
227
228x << 1
2291 << x
230x <<= 1