我在擺弄python的ctypes模塊以更好地理解垃圾收集器的工作原理。在翻譯的過程中,我遇到了這種奇怪的情況:參數計數器的奇怪行爲,整數爲0
>>>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)
爲什麼看起來myInt被Python引用了283次?我錯過了什麼嗎?
感謝您的見解。
僅供參考,您可以通過'sys.getrefcount'以不太古怪的方式獲得refcount。 'gc'模塊還包含幾個可用於解析GC行爲的函數。 – delnan
感謝您的信息! – Jokyjo