Message302072
Here is a not-much-more-complicated version that solves the problem. It is probably worth changing as the revised example makes clear the difference between self and obj, which is an important distinction.
class RevealAccess(object):
"""A data descriptor that sets and returns values
normally and prints a message logging their access.
"""
def __init__(self, initval=None, name='var'):
self.attrname = '_' + str(random.random())[2:]
self.name = name
self.initval = initval
def __get__(self, obj, objtype):
print('Retrieving', self.name)
return getattr(obj, self.attrname, self.initval)
def __set__(self, obj, val):
print('Updating', self.name)
setattr(obj, self.attrname, val)
class MyClass:
x = RevealAccess(10, 'var "x"')
y = 5 |
|
Date |
User |
Action |
Args |
2017-09-13 13:09:13 | r.david.murray | set | recipients:
+ r.david.murray, rhettinger, docs@python, Benjamin Wohlwend |
2017-09-13 13:09:13 | r.david.murray | set | messageid: <1505308153.33.0.117113013803.issue31441@psf.upfronthosting.co.za> |
2017-09-13 13:09:13 | r.david.murray | link | issue31441 messages |
2017-09-13 13:09:13 | r.david.murray | create | |
|