blob: a01195eae08802316907361d9ca25dd4cda6c6ba [file] [log] [blame]
Thomas Wouters104a7bc2000-08-24 20:14:10 +00001
2# Augmented assignment test.
3
4x = 2
5x += 1
6x *= 2
7x **= 2
8x -= 8
9x /= 2
10x %= 12
11x &= 2
12x |= 5
13x ^= 1
14
15print x
16
17x = [2]
18x[0] += 1
19x[0] *= 2
20x[0] **= 2
21x[0] -= 8
22x[0] /= 2
23x[0] %= 12
24x[0] &= 2
25x[0] |= 5
26x[0] ^= 1
27
28print x
29
30x = {0: 2}
31x[0] += 1
32x[0] *= 2
33x[0] **= 2
34x[0] -= 8
35x[0] /= 2
36x[0] %= 12
37x[0] &= 2
38x[0] |= 5
39x[0] ^= 1
40
41print x[0]
42
43x = [1,2]
44x += [3,4]
45x *= 2
46
47print x
48
49x = [1, 2, 3]
50y = x
51x[1:2] *= 2
52y[1:2] += [1]
53
54print x
55print x is y
56
57class aug_test:
58 def __init__(self, value):
59 self.val = value
60 def __radd__(self, val):
61 return self.val + val
62 def __add__(self, val):
63 return aug_test(self.val + val)
64
65
66class aug_test2(aug_test):
67 def __iadd__(self, val):
68 self.val = self.val + val
69 return self
70
71class aug_test3(aug_test):
72 def __iadd__(self, val):
73 return aug_test3(self.val + val)
74
75x = aug_test(1)
76y = x
77x += 10
78
79print isinstance(x, aug_test)
80print y is not x
81print x.val
82
83x = aug_test2(2)
84y = x
85x += 10
86
87print y is x
88print x.val
89
90x = aug_test3(3)
91y = x
92x += 10
93
94print isinstance(x, aug_test3)
95print y is not x
96print x.val
97
98class testall:
99
100 def __add__(self, val):
101 print "__add__ called"
102 def __radd__(self, val):
103 print "__radd__ called"
104 def __iadd__(self, val):
105 print "__iadd__ called"
106 return self
107
108 def __sub__(self, val):
109 print "__sub__ called"
110 def __rsub__(self, val):
111 print "__rsub__ called"
112 def __isub__(self, val):
113 print "__isub__ called"
114 return self
115
116 def __mul__(self, val):
117 print "__mul__ called"
118 def __rmul__(self, val):
119 print "__rmul__ called"
120 def __imul__(self, val):
121 print "__imul__ called"
122 return self
123
124 def __div__(self, val):
125 print "__div__ called"
126 def __rdiv__(self, val):
127 print "__rdiv__ called"
128 def __idiv__(self, val):
129 print "__idiv__ called"
130 return self
131
132 def __mod__(self, val):
133 print "__mod__ called"
134 def __rmod__(self, val):
135 print "__rmod__ called"
136 def __imod__(self, val):
137 print "__imod__ called"
138 return self
139
140 def __pow__(self, val):
141 print "__pow__ called"
142 def __rpow__(self, val):
143 print "__rpow__ called"
144 def __ipow__(self, val):
145 print "__ipow__ called"
146 return self
147
148 def __or__(self, val):
149 print "__or__ called"
150 def __ror__(self, val):
151 print "__ror__ called"
152 def __ior__(self, val):
153 print "__ior__ called"
154 return self
155
156 def __and__(self, val):
157 print "__and__ called"
158 def __rand__(self, val):
159 print "__rand__ called"
160 def __iand__(self, val):
161 print "__iand__ called"
162 return self
163
164 def __xor__(self, val):
165 print "__xor__ called"
166 def __rxor__(self, val):
167 print "__rxor__ called"
168 def __ixor__(self, val):
169 print "__ixor__ called"
170 return self
171
172 def __rshift__(self, val):
173 print "__rshift__ called"
174 def __rrshift__(self, val):
175 print "__rrshift__ called"
176 def __irshift__(self, val):
177 print "__irshift__ called"
178 return self
179
180 def __lshift__(self, val):
181 print "__lshift__ called"
182 def __rlshift__(self, val):
183 print "__rlshift__ called"
184 def __ilshift__(self, val):
185 print "__ilshift__ called"
186 return self
187
188x = testall()
189x + 1
1901 + x
191x += 1
192
193x - 1
1941 - x
195x -= 1
196
197x * 1
1981 * x
199x *= 1
200
201x / 1
2021 / x
203x /= 1
204
205x % 1
2061 % x
207x %= 1
208
209x ** 1
2101 ** x
211x **= 1
212
213x | 1
2141 | x
215x |= 1
216
217x & 1
2181 & x
219x &= 1
220
221x ^ 1
2221 ^ x
223x ^= 1
224
225x >> 1
2261 >> x
227x >>= 1
228
229x << 1
2301 << x
231x <<= 1
232