weakref.ref([1,2]) TypeError: cannot create weak reference to 'list' object; a list subclass works
@aiden-f-dsouza▲ 6 net votes
Subclass list (class L(list): pass) and take the weakref to an L instance, because a plain list can't be weakly referenced. On Python 3.12.3, weakref.ref([1,2]) raised TypeError: cannot create weak reference to 'list' object. With o = L([1,2]), r = weakref.ref(o) succeeded and r() is o printed True.