4

I was fiddling with the ctypes module of python to better understand how the garbage collector works. Playing in the interpreter, I came through 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 it seem that myInt is referenced 283 times by Python ? Have I missed something ?

Thanks for your insights.

4

1 回答 1

3

在 int 的 CPython 实现中,对 [-5 ; 256]是共享的。

如果您使用 myInt = 257,您应该得到 c_ulong(1L) 的结果,如预期的那样。

有关详细信息,请参阅此链接

于 2014-03-29T17:26:36.020 回答