Code Explanation
import weakref
Imports the weakref module, which allows creating weak references to objects.
class A: pass
Defines a simple class A with no attributes or methods.
obj = A()
Creates an instance obj of class A, which is a strong reference to the object.
Since obj is a strong reference, the object will not be garbage collected while obj exists.
wref = weakref.ref(obj)
Creates a weak reference wref to obj using weakref.ref(obj).
A weak reference does not prevent garbage collection of the object.
wref is a callable weak reference, meaning we must call wref() to access the original object.
print(wref() is obj)
Checks if wref() returns the same object as obj.
wref() calls the weak reference and returns the original object (obj).
Since obj is still alive, wref() returns the same object as obj
0 Comments:
Post a Comment