So when I create an object without reference (as far as I can tell) why does Python maintain that there's a refcount for it?
>>> import sys
>>> sys.getrefcount(object())
1
The following makes sense based on this extra refcount.
>>> o = object()
>>> sys.getrefcount(o)
2
>>> l = list((o,))
>>> sys.getrefcount(o)
3
>>> del l[0]
>>> sys.getrefcount(o)
2
And it would seem that on
>>> del o
Python would Garbage Collect the object, but does it? If there's still a reference to it, where is that?
When you invoke sys.getrefcount()
, a reference is generated for local use inside the function. getrefcount()
will always add 1 to the actual count, because it counts its own internal ref.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With