Skip to content

Instantly share code, notes, and snippets.

@dichharai
Last active October 24, 2022 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dichharai/b3eb92ca13b61d9a98fa3ef60d743240 to your computer and use it in GitHub Desktop.
Save dichharai/b3eb92ca13b61d9a98fa3ef60d743240 to your computer and use it in GitHub Desktop.
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