Weak Reference
say it: “WEEK REF-er-ens” /ˈwik ˈrɛfərəns/
Points to an object without keeping it alive.
What is a weak reference?
A weak reference points to an object without keeping it alive. Once nothing else uses the object it is freed, and the weak reference gives back None. It's useful for caches.
import weakref
class Photo:
pass
photo = Photo()
ref = weakref.ref(photo)
print(ref() is photo) # True
del photo
print(ref()) # None: the photo is gone
Level: Master · More Python terms at this level
Updated