2024-04-19 12:28:05 发布
网友
根据Pythondocumentation,sys模块包含一个函数:
sys
import sys sys.getrefcount(object) #-- Returns the reference count of the object.
由于对象arg temp引用,通常比预期的高1。
使用垃圾收集器guts的接口gc模块,您可以调用gc.get_referrers(foo)来获取引用foo的所有内容的列表。
gc
gc.get_referrers(foo)
foo
因此,len(gc.get_referrers(foo))将给出该列表的长度:引用者的数量,这是您所追求的。
len(gc.get_referrers(foo))
另请参见^{} module documentation。
有gc.get_referrers()和sys.getrefcount()。但是,很难看出sys.getrefcount(X)是如何服务于传统的引用计数的。考虑:
gc.get_referrers()
sys.getrefcount()
sys.getrefcount(X)
import sys def function(X): sub_function(X) def sub_function(X): sub_sub_function(X) def sub_sub_function(X): print sys.getrefcount(X)
然后function(SomeObject)传递'7',sub_function(SomeObject)提供“5”,sub_sub_function(SomeObject)提供“3”,并且sys.getrefcount(SomeObject)传递“2”。
function(SomeObject)
sub_function(SomeObject)
sub_sub_function(SomeObject)
sys.getrefcount(SomeObject)
换句话说:如果您使用sys.getrefcount(),那么您必须知道函数调用深度。对于gc.get_referrers(),可能需要过滤引用列表。
我建议进行手动引用计数,例如“更改时隔离”,即“在其他地方引用时克隆”。
根据Pythondocumentation,
sys
模块包含一个函数:由于对象arg temp引用,通常比预期的高1。
使用垃圾收集器guts的接口
gc
模块,您可以调用gc.get_referrers(foo)
来获取引用foo
的所有内容的列表。因此,
len(gc.get_referrers(foo))
将给出该列表的长度:引用者的数量,这是您所追求的。另请参见^{} module documentation 。
有
gc.get_referrers()
和sys.getrefcount()
。但是,很难看出sys.getrefcount(X)
是如何服务于传统的引用计数的。考虑:然后
function(SomeObject)
传递'7',sub_function(SomeObject)
提供“5”,sub_sub_function(SomeObject)
提供“3”,并且sys.getrefcount(SomeObject)
传递“2”。换句话说:如果您使用
sys.getrefcount()
,那么您必须知道函数调用深度。对于gc.get_referrers()
,可能需要过滤引用列表。我建议进行手动引用计数,例如“更改时隔离”,即“在其他地方引用时克隆”。
相关问题 更多 >
编程相关推荐