Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. class RevealAccess(object):
  2.     """A data descriptor that sets and returns values
  3.       normally and prints a message logging their access.
  4.    """
  5.  
  6.     def __init__(self, initval=None, name='var'):
  7.         self.val = initval
  8.         self.name = name
  9.  
  10.     def __get__(self, obj, objtype):
  11.         print 'Retrieving', self.name
  12.         return self.val
  13.  
  14.     def __set__(self, obj, val):
  15.         print 'Updating' , self.name
  16.         self.val = val
  17.  
  18. class MyClass(object):
  19.     x = RevealAccess(10, 'var "x"')
  20.     y = 5
  21.  
  22. m = MyClass()
  23. print m.x
  24. m.x = 20
  25. print m.x
  26. print m.y
  27.  
  28.  
  29. m2 = MyClass()
  30. print m2.x
  31. m2.x = 10
  32. print m.x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement