Created
June 8, 2018 14:24
-
-
Save acwoss/de739e280e6206b461db4fb08aa00ce9 to your computer and use it in GitHub Desktop.
ForestgreenMarriedGravity created by acwoss - https://repl.it/@acwoss/ForestgreenMarriedGravity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment