Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame^] | 1 | |
| 2 | # Augmented assignment test. |
| 3 | |
| 4 | x = 2 |
| 5 | x += 1 |
| 6 | x *= 2 |
| 7 | x **= 2 |
| 8 | x -= 8 |
| 9 | x /= 2 |
| 10 | x %= 12 |
| 11 | x &= 2 |
| 12 | x |= 5 |
| 13 | x ^= 1 |
| 14 | |
| 15 | print x |
| 16 | |
| 17 | x = [2] |
| 18 | x[0] += 1 |
| 19 | x[0] *= 2 |
| 20 | x[0] **= 2 |
| 21 | x[0] -= 8 |
| 22 | x[0] /= 2 |
| 23 | x[0] %= 12 |
| 24 | x[0] &= 2 |
| 25 | x[0] |= 5 |
| 26 | x[0] ^= 1 |
| 27 | |
| 28 | print x |
| 29 | |
| 30 | x = {0: 2} |
| 31 | x[0] += 1 |
| 32 | x[0] *= 2 |
| 33 | x[0] **= 2 |
| 34 | x[0] -= 8 |
| 35 | x[0] /= 2 |
| 36 | x[0] %= 12 |
| 37 | x[0] &= 2 |
| 38 | x[0] |= 5 |
| 39 | x[0] ^= 1 |
| 40 | |
| 41 | print x[0] |
| 42 | |
| 43 | x = [1,2] |
| 44 | x += [3,4] |
| 45 | x *= 2 |
| 46 | |
| 47 | print x |
| 48 | |
| 49 | x = [1, 2, 3] |
| 50 | y = x |
| 51 | x[1:2] *= 2 |
| 52 | y[1:2] += [1] |
| 53 | |
| 54 | print x |
| 55 | print x is y |
| 56 | |
| 57 | class 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 | |
| 66 | class aug_test2(aug_test): |
| 67 | def __iadd__(self, val): |
| 68 | self.val = self.val + val |
| 69 | return self |
| 70 | |
| 71 | class aug_test3(aug_test): |
| 72 | def __iadd__(self, val): |
| 73 | return aug_test3(self.val + val) |
| 74 | |
| 75 | x = aug_test(1) |
| 76 | y = x |
| 77 | x += 10 |
| 78 | |
| 79 | print isinstance(x, aug_test) |
| 80 | print y is not x |
| 81 | print x.val |
| 82 | |
| 83 | x = aug_test2(2) |
| 84 | y = x |
| 85 | x += 10 |
| 86 | |
| 87 | print y is x |
| 88 | print x.val |
| 89 | |
| 90 | x = aug_test3(3) |
| 91 | y = x |
| 92 | x += 10 |
| 93 | |
| 94 | print isinstance(x, aug_test3) |
| 95 | print y is not x |
| 96 | print x.val |
| 97 | |
| 98 | class 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 | |
| 188 | x = testall() |
| 189 | x + 1 |
| 190 | 1 + x |
| 191 | x += 1 |
| 192 | |
| 193 | x - 1 |
| 194 | 1 - x |
| 195 | x -= 1 |
| 196 | |
| 197 | x * 1 |
| 198 | 1 * x |
| 199 | x *= 1 |
| 200 | |
| 201 | x / 1 |
| 202 | 1 / x |
| 203 | x /= 1 |
| 204 | |
| 205 | x % 1 |
| 206 | 1 % x |
| 207 | x %= 1 |
| 208 | |
| 209 | x ** 1 |
| 210 | 1 ** x |
| 211 | x **= 1 |
| 212 | |
| 213 | x | 1 |
| 214 | 1 | x |
| 215 | x |= 1 |
| 216 | |
| 217 | x & 1 |
| 218 | 1 & x |
| 219 | x &= 1 |
| 220 | |
| 221 | x ^ 1 |
| 222 | 1 ^ x |
| 223 | x ^= 1 |
| 224 | |
| 225 | x >> 1 |
| 226 | 1 >> x |
| 227 | x >>= 1 |
| 228 | |
| 229 | x << 1 |
| 230 | 1 << x |
| 231 | x <<= 1 |
| 232 | |