2021-11-08 | Paul Boddie | file changeset files shortlog | Introduced copyable and mutable flags in the tagged region of attribute values. Such flags are set when new integer and floating point values are created, but the mutable flag is cleared when such attributes are propagated between functions in order to prevent values being replaced upon assignment to parameter names, this being a concern when value copying is introduced. | tagged-address-values |
paul@364 | 1 | class A: |
paul@364 | 2 | def __init__(self, x): |
paul@364 | 3 | self.x = x |
paul@364 | 4 | def __sub__(self, other): |
paul@364 | 5 | return self.x - other.x |
paul@364 | 6 | |
paul@364 | 7 | class B: |
paul@364 | 8 | def __init__(self, x): |
paul@364 | 9 | self.x = x |
paul@364 | 10 | def __rsub__(self, other): |
paul@364 | 11 | return other.x - self.x |
paul@364 | 12 | |
paul@364 | 13 | class C: |
paul@364 | 14 | def __init__(self, x): |
paul@364 | 15 | self.x = x |
paul@364 | 16 | |
paul@364 | 17 | a = A(10) |
paul@364 | 18 | b = B(5) |
paul@364 | 19 | c = C(3) |
paul@364 | 20 | |
paul@364 | 21 | print a - b # 5 |
paul@364 | 22 | print c - b # -2 |
paul@364 | 23 | print a - c # 7 |
paul@364 | 24 | |
paul@364 | 25 | try: |
paul@364 | 26 | print b - c # should raise an exception |
paul@364 | 27 | except TypeError: |
paul@364 | 28 | print "b - c: b and c do not respectively support the __sub__ and __rsub__ operations" |