blob: 22cca44e99a7176f2cd5ff3e3096607d78634c15 [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
Thomas Wouters104a7bc2000-08-24 20:14:10 +00008x %= 12
Thomas Wouters34729032006-04-15 09:10:43 +00009x >>= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +000010x &= 2
11x |= 5
12x ^= 1
Thomas Wouters34729032006-04-15 09:10:43 +000013x <<= 2
14x /= 2
15x //= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000016
17print x
Thomas Wouters34729032006-04-15 09:10:43 +000018print int(x)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000019
20x = [2]
21x[0] += 1
22x[0] *= 2
23x[0] **= 2
24x[0] -= 8
Thomas Wouters104a7bc2000-08-24 20:14:10 +000025x[0] %= 12
Thomas Wouters34729032006-04-15 09:10:43 +000026x[0] >>= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +000027x[0] &= 2
28x[0] |= 5
29x[0] ^= 1
Thomas Wouters34729032006-04-15 09:10:43 +000030x[0] <<= 2
31x[0] /= 2
32x[0] //= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000033
34print x
Thomas Wouters34729032006-04-15 09:10:43 +000035print int(x[0])
Thomas Wouters104a7bc2000-08-24 20:14:10 +000036
37x = {0: 2}
38x[0] += 1
39x[0] *= 2
40x[0] **= 2
41x[0] -= 8
Thomas Wouters104a7bc2000-08-24 20:14:10 +000042x[0] %= 12
Thomas Wouters34729032006-04-15 09:10:43 +000043x[0] >>= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +000044x[0] &= 2
45x[0] |= 5
46x[0] ^= 1
Thomas Wouters34729032006-04-15 09:10:43 +000047x[0] <<= 2
48x[0] /= 2
49x[0] //= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000050
51print x[0]
Thomas Wouters34729032006-04-15 09:10:43 +000052print int(x[0])
Thomas Wouters104a7bc2000-08-24 20:14:10 +000053
54x = [1,2]
55x += [3,4]
56x *= 2
57
58print x
59
60x = [1, 2, 3]
61y = x
62x[1:2] *= 2
63y[1:2] += [1]
64
65print x
66print x is y
67
68class aug_test:
Fred Drake004d5e62000-10-23 17:22:08 +000069 def __init__(self, value):
70 self.val = value
71 def __radd__(self, val):
72 return self.val + val
73 def __add__(self, val):
74 return aug_test(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000075
76
77class aug_test2(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000078 def __iadd__(self, val):
79 self.val = self.val + val
80 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +000081
82class aug_test3(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000083 def __iadd__(self, val):
84 return aug_test3(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000085
86x = aug_test(1)
87y = x
88x += 10
89
90print isinstance(x, aug_test)
Fred Drake004d5e62000-10-23 17:22:08 +000091print y is not x
Thomas Wouters104a7bc2000-08-24 20:14:10 +000092print x.val
93
94x = aug_test2(2)
95y = x
96x += 10
97
98print y is x
99print x.val
100
101x = aug_test3(3)
102y = x
103x += 10
104
105print isinstance(x, aug_test3)
106print y is not x
107print x.val
108
109class testall:
110
Fred Drake004d5e62000-10-23 17:22:08 +0000111 def __add__(self, val):
112 print "__add__ called"
113 def __radd__(self, val):
114 print "__radd__ called"
115 def __iadd__(self, val):
116 print "__iadd__ called"
117 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000118
Fred Drake004d5e62000-10-23 17:22:08 +0000119 def __sub__(self, val):
120 print "__sub__ called"
121 def __rsub__(self, val):
122 print "__rsub__ called"
123 def __isub__(self, val):
124 print "__isub__ called"
125 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000126
Fred Drake004d5e62000-10-23 17:22:08 +0000127 def __mul__(self, val):
128 print "__mul__ called"
129 def __rmul__(self, val):
130 print "__rmul__ called"
131 def __imul__(self, val):
132 print "__imul__ called"
133 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000134
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000135 def __floordiv__(self, val):
136 print "__floordiv__ called"
137 return self
138 def __ifloordiv__(self, val):
139 print "__ifloordiv__ called"
140 return self
141 def __rfloordiv__(self, val):
142 print "__rfloordiv__ called"
143 return self
144
145 def __truediv__(self, val):
146 print "__truediv__ called"
147 return self
148 def __itruediv__(self, val):
149 print "__itruediv__ called"
150 return self
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000151 def __rtruediv__(self, val):
152 print "__rtruediv__ called"
153 return self
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000154
Fred Drake004d5e62000-10-23 17:22:08 +0000155 def __mod__(self, val):
156 print "__mod__ called"
157 def __rmod__(self, val):
158 print "__rmod__ called"
159 def __imod__(self, val):
160 print "__imod__ called"
161 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000162
Fred Drake004d5e62000-10-23 17:22:08 +0000163 def __pow__(self, val):
164 print "__pow__ called"
165 def __rpow__(self, val):
166 print "__rpow__ called"
167 def __ipow__(self, val):
168 print "__ipow__ called"
169 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000170
Fred Drake004d5e62000-10-23 17:22:08 +0000171 def __or__(self, val):
172 print "__or__ called"
173 def __ror__(self, val):
174 print "__ror__ called"
175 def __ior__(self, val):
176 print "__ior__ called"
177 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000178
Fred Drake004d5e62000-10-23 17:22:08 +0000179 def __and__(self, val):
180 print "__and__ called"
181 def __rand__(self, val):
182 print "__rand__ called"
183 def __iand__(self, val):
184 print "__iand__ called"
185 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000186
Fred Drake004d5e62000-10-23 17:22:08 +0000187 def __xor__(self, val):
188 print "__xor__ called"
189 def __rxor__(self, val):
190 print "__rxor__ called"
191 def __ixor__(self, val):
192 print "__ixor__ called"
193 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000194
Fred Drake004d5e62000-10-23 17:22:08 +0000195 def __rshift__(self, val):
196 print "__rshift__ called"
197 def __rrshift__(self, val):
198 print "__rrshift__ called"
199 def __irshift__(self, val):
200 print "__irshift__ called"
201 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000202
Fred Drake004d5e62000-10-23 17:22:08 +0000203 def __lshift__(self, val):
204 print "__lshift__ called"
205 def __rlshift__(self, val):
206 print "__rlshift__ called"
207 def __ilshift__(self, val):
208 print "__ilshift__ called"
209 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000210
211x = testall()
212x + 1
2131 + x
214x += 1
215
216x - 1
2171 - x
218x -= 1
219
220x * 1
2211 * x
222x *= 1
223
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000224x / 1
2251 / x
226x /= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000227
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000228x // 1
2291 // x
230x //= 1
231
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000232x % 1
2331 % x
234x %= 1
235
236x ** 1
2371 ** x
238x **= 1
239
240x | 1
2411 | x
242x |= 1
243
244x & 1
2451 & x
246x &= 1
247
248x ^ 1
2491 ^ x
250x ^= 1
251
252x >> 1
2531 >> x
254x >>= 1
255
256x << 1
2571 << x
258x <<= 1