Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- m2 = MyClass()
- print m2.x
- m2.x = 10
- print m.x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement