Hanno Rein on Nostr: Can someone help me understand what's going on with reference counts during a #python ...
Can someone help me understand what's going on with reference counts during a #python exception in a #jupyter notebook?
# Cell 1
import sys
class Test:
def exception(self):
raise ValueError("test")
t = Test()
print(sys.getrefcount(t)) # -> 2
t.exception()
# Cell 2
print(sys.getrefcount(t)) # -> 4
Why does the reference count remain increased after an exception? Is there a way to avoid this?
Published at
2024-12-22 18:27:22Event JSON
{
"id": "72cf72c1796e2df9d232b8e6086491c63855169424681995f8a6479cffe658c8",
"pubkey": "180141b0eb1a89c8e210cf4ea941fb70f70a5a32d96f52b556d2f97cb8ee2daf",
"created_at": 1734892042,
"kind": 1,
"tags": [
[
"t",
"python"
],
[
"t",
"jupyter"
],
[
"proxy",
"https://mastodon.social/users/hannorein/statuses/113697884922216639",
"activitypub"
]
],
"content": "Can someone help me understand what's going on with reference counts during a #python exception in a #jupyter notebook? \n\n# Cell 1\nimport sys\nclass Test:\n def exception(self):\n raise ValueError(\"test\")\nt = Test()\nprint(sys.getrefcount(t)) # -\u003e 2\nt.exception() \n# Cell 2\nprint(sys.getrefcount(t)) # -\u003e 4\n\nWhy does the reference count remain increased after an exception? Is there a way to avoid this?",
"sig": "9793791cb950072e33621ee756bfd84d570c130431e0f59171489bd7ffa579d947debc8eb5880edde54316c9dc58d81d79b541a1de0bd3775990b132bc28f138"
}