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 C: 2 def __init__(self): 3 self.x = 123 4 5 def f(self): 6 return self.x 7 8 class D: 9 pass 10 11 c = C() 12 f = C.f 13 fn = get_using(C.f, c) 14 print fn # __main__.C.f 15 print fn() # 123 16 17 fn = get_using(C.f, C) 18 print fn # __main__.C.f 19 try: 20 print fn() # fails 21 except UnboundMethodInvocation: 22 print "fn(): method is unbound" 23 24 try: 25 print f() # fails 26 except UnboundMethodInvocation: 27 print "f(): method is unbound" 28 29 d = D() 30 try: 31 fn = get_using(C.f, d) 32 except TypeError: 33 print "get_using(C.f, d): d is not compatible with C" 34 35 fn = get_using(c, C.f) 36 print fn # <__main__.C instance> 37 try: 38 print fn() # fails 39 except TypeError: 40 print "fn(): object is not callable"