2019-05-20 | Paul Boddie | file changeset files shortlog | 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 |
paul@2 | 1 | class C: |
paul@214 | 2 | l = [2, 4, 6, 8, 10] |
paul@2 | 3 | s = "test" |
paul@2 | 4 | def __init__(self, x): |
paul@2 | 5 | self.x = x |
paul@2 | 6 | self.y = 3 |
paul@214 | 7 | self.z = "zebra libre" |
paul@840 | 8 | def __len__(self): |
paul@840 | 9 | return len(self.z) |
paul@2 | 10 | |
paul@2 | 11 | c = C([1]) |
paul@2 | 12 | x = c.x |
paul@2 | 13 | f = c.x.__len__ |
paul@266 | 14 | print c # <__main__.C instance> |
paul@266 | 15 | print x # [1] |
paul@266 | 16 | print f # __builtins__.list.list.__len__ |
paul@266 | 17 | print f() # 1 |
paul@2 | 18 | |
paul@2 | 19 | y = c.l |
paul@2 | 20 | g = c.l.__len__ |
paul@266 | 21 | print y # [2, 4, 6, 8, 10] |
paul@266 | 22 | print g # __builtins__.list.list.__len__ |
paul@266 | 23 | print g() # 5 |
paul@2 | 24 | |
paul@2 | 25 | yy = C.l |
paul@2 | 26 | gg = C.l.__len__ |
paul@266 | 27 | print yy # [2, 4, 6, 8, 10] |
paul@266 | 28 | print gg # __builtins__.list.list.__len__ |
paul@266 | 29 | print gg() # 5 |
paul@2 | 30 | |
paul@2 | 31 | z = c.s |
paul@2 | 32 | h = c.s.__len__ |
paul@266 | 33 | print z # test |
paul@266 | 34 | print h # __builtins__.str.basestring.__len__ |
paul@266 | 35 | print h() # 4 |
paul@2 | 36 | |
paul@2 | 37 | zz = C.s |
paul@2 | 38 | hh = C.s.__len__ |
paul@266 | 39 | print zz # test |
paul@266 | 40 | print hh # __builtins__.str.basestring.__len__ |
paul@266 | 41 | print hh() # 4 |
paul@2 | 42 | |
paul@2 | 43 | a = c.y |
paul@2 | 44 | b = c.z |
paul@2 | 45 | i = c.z.__len__ |
paul@266 | 46 | print a # 3 |
paul@266 | 47 | print b # zebra libre |
paul@266 | 48 | print i # __builtins__.str.basestring.__len__ |
paul@266 | 49 | print i() # 11 |
paul@840 | 50 | |
paul@840 | 51 | j = C.__len__ |
paul@840 | 52 | k = get_using(j, c) |
paul@840 | 53 | try: |
paul@840 | 54 | print j() |
paul@840 | 55 | except UnboundMethodInvocation: |
paul@840 | 56 | print "j(): invocation of method with class context" |
paul@840 | 57 | print k() # 11 |