quixadhal

testing set getters/setters

Aug 5th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import sys
  2.  
  3. class thing():
  4.     def __init__(self):
  5.         self._stuff = { 'foo', 'bar' }
  6.  
  7.     @property
  8.     def foo(self):
  9.         return True if sys._getframe().f_code.co_name in self._stuff else False
  10.  
  11.     @property
  12.     def bar(self):
  13.         return True if sys._getframe().f_code.co_name in self._stuff else False
  14.  
  15.     @foo.setter
  16.     def foo(self, v):
  17.         if v:
  18.             self._stuff.add(sys._getframe().f_code.co_name)
  19.         else:
  20.             self._stuff.discard(sys._getframe().f_code.co_name)
  21.  
  22.     @bar.setter
  23.     def bar(self, v):
  24.         if v:
  25.             self._stuff.add(sys._getframe().f_code.co_name)
  26.         else:
  27.             self._stuff.discard(sys._getframe().f_code.co_name)
  28.  
  29.     def __repr__(self):
  30.         return ', '.join(x for x in self._stuff)
  31.  
  32. x = thing()
  33. print('foo %s, bar %s' % (x.foo, x.bar))
  34. x.foo = True
  35. print('foo %s, bar %s' % (x.foo, x.bar))
  36. x.foo = False
  37. print('foo %s, bar %s' % (x.foo, x.bar))
  38. x.bar = False
  39. print('foo %s, bar %s' % (x.foo, x.bar))
  40. x.foo = True
  41. x.bar = True
  42. print('foo %s, bar %s' % (x.foo, x.bar))
  43.  
  44. print(x)
Advertisement
Add Comment
Please, Sign In to add comment