Implementing weak references in Python

Normal Python references to objects increment the object’s reference count thus preventing it from being garbage collected. If a user desires creating weak references, the weakref module can be used: import weakref class Rezha(object): pass To create a weak reference, the ref class is used: # object instance rezha = Rezha() # weak reference to our object r = weakref.ref(rezha) Then, you can call the reference object: print(r) # <weakref at 0x01414E40; to 'Rezha'....

April 30, 2017 · 1 min · Rezha Julio