Public paste
Undefined
By: Guest | Date: Nov 9 2018 10:37 | Format: Python | Expires: 18 days | Size: 357 B | Hits: 197

  1. # weaktest.py
  2.  
  3. import weakref
  4.  
  5. class C:
  6.     pass
  7.  
  8. def strong():
  9.     x = C()
  10.     d = {'x': x}
  11.     return d
  12.  
  13. def weak():
  14.     x = C()
  15.     d = weakref.WeakValueDictionary({'x': x})
  16.     return d
  17.  
  18. print("strong survived:", len(strong()))
  19. print("weak survived:", len(weak()))
  20.  
  21. # $ python3 weaktest.py
  22. # strong survived: 1
  23. # weak survived: 0
  24.