Advertisement
Guest User

Descriptor example using weak key dictionary

a guest
Dec 30th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. import weakref
  2.  
  3. class Foo:
  4.     def __init__(self):
  5.         self._data = weakref.WeakKeyDictionary()
  6.     def __get__(self, obj, owner):
  7.         if obj is None:
  8.             return self
  9.         result = self._data.get(obj, None)
  10.         # do something to result
  11.         return result
  12.     def __set__(self, obj, value):
  13.         # do something to value
  14.         self._data[obj] = value
  15.     def __delete__(self, obj):
  16.         del self._data[obj]
  17.  
  18. class Bar:
  19.     foo = Foo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement