SHARE
TWEET

Descriptor example using weak key dictionary

a guest Dec 30th, 2014 230 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top