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 class D: 3 class E: 4 def m(self, x): 5 self.x = x 6 return self.x.__len__ 7 n = 123 8 o = "123" 9 10 p = "456" 11 q = 789 12 13 class F(E): 14 def r(self, y): 15 s = self.o 16 C.D.F.t = 234 17 return self.o.__len__ 18 t = 123 19 def u(self): 20 return self.o 21 def v(self): 22 return self.u().__len__ 23 24 def static(): 25 c = C 26 d = C.D 27 e = C.D.E 28 f = C.D.E.m 29 g = C.D.E.n 30 h = C.D.p 31 32 print c # __main__.C 33 print d # __main__.C.D 34 print e # __main__.C.D.E 35 print f # __main__.C.D.E.m 36 print g # 123 37 print h # 456 38 39 def static_via_constant(): 40 i = C.D.p.__len__ 41 42 print i # __builtins__.str.basestring.bytelength 43 44 def assign(): 45 C.D.q = 987 46 47 def indirect(): 48 e = C.D.E 49 inst = e() 50 method = inst.m 51 return method("5") 52 53 def broken(): 54 inst2 = C.D.F() 55 l = inst2.u().__len__ 56 return l 57 58 static() 59 static_via_constant() 60 assign() 61 print indirect() # __builtins__.str.basestring.bytelength 62 print indirect()() # 1 63 print broken() # __builtins__.str.basestring.bytelength 64 print broken()() # 3 65 66 print C.D.q # 987 67 68 # Static chains. 69 70 c = C 71 d = C.D 72 e = C.D.E 73 f = C.D.E.m 74 g = C.D.E.n 75 h = C.D.p 76 77 print c # __main__.C 78 print d # __main__.C.D 79 print e # __main__.C.D.E 80 print f # __main__.C.D.E.m 81 print g # 123 82 print h # "456" 83 84 # Static via constant. 85 86 i = C.D.p.__len__ 87 88 print i # __builtins__.str.basestring.bytelength 89 print i() # 3 90 91 # Static assignment. 92 93 C.D.q = 654 94 95 print C.D.q # 654 96 97 # Indirect accesses. 98 99 inst = e() 100 method = inst.m 101 print method("5") # __builtins__.str.basestring.bytelength 102 print method("5")() # 1 103 104 # Broken chains. 105 106 inst2 = C.D.F() 107 l = inst2.u().__len__ 108 print l # __builtins__.str.basestring.bytelength 109 print l() # 3