2019-05-20 | Paul Boddie | raw annotate files changeset graph | Experiment with a temporary attribute on objects. This attribute must be cleared when assignments occur, but it should also be cleared when objects escape from functions (which is not yet done). Since the motivation for marking objects as temporary is their re-use in certain operations, the objective is to avoid objects being used in multiple places and still being marked as temporary and potentially undergoing re-use/mutation. Perhaps the only way of guaranteeing exclusive access to an object is if it is provided as a function result and is immediately passed to a function or operation, with this function either mutating the object or ensuring its direct propagation to another such function. | temporary-object-experiment |
1 class A: 2 def __init__(self, x): 3 self.x = x 4 def __sub__(self, other): 5 return self.x - other.x 6 7 class B: 8 def __init__(self, x): 9 self.x = x 10 def __rsub__(self, other): 11 return other.x - self.x 12 13 class C: 14 def __init__(self, x): 15 self.x = x 16 17 a = A(10) 18 b = B(5) 19 c = C(3) 20 21 print a - b # 5 22 print c - b # -2 23 print a - c # 7 24 25 try: 26 print b - c # should raise an exception 27 except TypeError: 28 print "b - c: b and c do not respectively support the __sub__ and __rsub__ operations"