Strange behavior of ref counter with ints 0

I played with the python ctypes module to better understand how the garbage collector works. While playing in the interpreter, I came across this strange situation:

>>>import ctypes
>>>def get_ref(obj):
...    """ This returns the refcount of obj as a c_size_t """
...    return ctypes.c_size_t.from_address(id(obj))
...
>>>myInt = 0
>>>get_ref(myInt)
c_ulong(283L)

Why does myInt seem to refer to Python 283 times? Did I miss something?

Thanks for your ideas.

+4
source share
1 answer

In the CPython implementation for int, references to [-5; 256].

If you use myInt = 257, you should get the result c_ulong (1L), as expected.

See the link for more details.

+3
source

Source: https://habr.com/ru/post/1534057/


All Articles