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.val = initval
self.name = name
def __get__(self, obj, objtype):
print('Retrieving', self.name)
return self.val
def __set__(self, obj, val):
print('Updating', self.name)
self.val = val
class MyClass(object):
x = RevealAccess(10, 'var "x"')
y = 5
m = MyClass()
print(m.x)
m.x = 20
print(m.x)
print(m.y)
Y2xhc3MgUmV2ZWFsQWNjZXNzKG9iamVjdCk6CiAgICAiIiJBIGRhdGEgZGVzY3JpcHRvciB0aGF0IHNldHMgYW5kIHJldHVybnMgdmFsdWVzCiAgICAgICBub3JtYWxseSBhbmQgcHJpbnRzIGEgbWVzc2FnZSBsb2dnaW5nIHRoZWlyIGFjY2Vzcy4KICAgICIiIgoKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBpbml0dmFsPU5vbmUsIG5hbWU9J3ZhcicpOgogICAgICAgIHNlbGYudmFsID0gaW5pdHZhbAogICAgICAgIHNlbGYubmFtZSA9IG5hbWUKCiAgICBkZWYgX19nZXRfXyhzZWxmLCBvYmosIG9ianR5cGUpOgogICAgICAgIHByaW50KCdSZXRyaWV2aW5nJywgc2VsZi5uYW1lKQogICAgICAgIHJldHVybiBzZWxmLnZhbAoKICAgIGRlZiBfX3NldF9fKHNlbGYsIG9iaiwgdmFsKToKICAgICAgICBwcmludCgnVXBkYXRpbmcnLCBzZWxmLm5hbWUpCiAgICAgICAgc2VsZi52YWwgPSB2YWwKCmNsYXNzIE15Q2xhc3Mob2JqZWN0KToKICAgIHggPSBSZXZlYWxBY2Nlc3MoMTAsICd2YXIgIngiJykKICAgIHkgPSA1CgptID0gTXlDbGFzcygpCgpwcmludChtLngpCgptLnggPSAyMAoKcHJpbnQobS54KQpwcmludChtLnkp