Last active
October 24, 2022 06:28
-
-
Save dichharai/b3eb92ca13b61d9a98fa3ef60d743240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import weakref | |
import gc | |
class Serval: | |
def __init__(self, name, cousin = None): | |
self.name = name | |
self.cousin = cousin | |
class Cat: | |
def __init__(self, name, cousin=None): | |
self.name = name | |
self.cousin = cousin | |
servy = Serval("Servy") | |
whiky = Cat("Whiky", weakref.ref(servy)) | |
# servy.cousin = whiky | |
servy.cousin = weakref.ref(whiky) | |
print(f"servy's refcount: {ctypes.c_long.from_address(id(servy)).value}") | |
print(f"whiky's refcount: {ctypes.c_long.from_address(id(whiky)).value}") | |
del servy | |
gc.collect() | |
print("AFTER DELETION OF servy") | |
print(f"whiky's refcount: {ctypes.c_long.from_address(id(whiky)).value}") | |
print(f"whiky's cousin: {whiky.cousin()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment