有没有办法在Python中获取对象的当前引用计数?

发布于 2024-07-12 23:26:48 字数 32 浏览 4 评论 0原文

有没有办法在Python中获取对象的当前引用计数?

Is there a way to get the current ref count of an object in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

行至春深 2024-07-19 23:26:49

根据Python 文档sys 模块包含一个函数:

import sys
sys.getrefcount(object) #-- Returns the reference count of the object.

通常比您预期的高 1,因为对象 arg 临时引用。

According to the Python documentation, the sys module contains a function:

import sys
sys.getrefcount(object) #-- Returns the reference count of the object.

Generally 1 higher than you might expect, because of object arg temp reference.

丑丑阿 2024-07-19 23:26:49

使用 gc 模块(垃圾收集器内部的接口),您可以调用 gc.get_referrers(foo) 来获取引用 foo 的所有内容的列表代码>.

因此, len(gc.get_referrers(foo)) 将为您提供该列表的长度:引用者的数量,这就是您想要的。

另请参阅 gc 模块文档

Using the gc module, the interface to the garbage collector guts, you can call gc.get_referrers(foo) to get a list of everything referring to foo.

Hence, len(gc.get_referrers(foo)) will give you the length of that list: the number of referrers, which is what you're after.

See also the gc module documentation.

ㄟ。诗瑗 2024-07-19 23:26:49

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”。

换句话说:如果您使用 sys.getrefcount() ,您必须了解函数调用深度。 对于 gc.get_referrers() ,人们可能必须过滤引用者列表。

我建议出于“更改隔离”等目的进行手动引用计数,即“如果在其他地方引用则进行克隆”。

There is gc.get_referrers() and sys.getrefcount(). But, It is kind of hard to see how sys.getrefcount(X) could serve the purpose of traditional reference counting. Consider:

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)

Then function(SomeObject) delivers '7',
sub_function(SomeObject) delivers '5',
sub_sub_function(SomeObject) delivers '3', and
sys.getrefcount(SomeObject) delivers '2'.

In other words: If you use sys.getrefcount() you must be aware of the function call depth. For gc.get_referrers() one might have to filter the list of referrers.

I would propose to do manual reference counting for purposes such as “isolation on change”, i.e. “clone if referenced elsewhere”.

话少心凉 2024-07-19 23:26:49
import ctypes

my_var = 'hello python'
my_var_address = id(my_var)

ctypes.c_long.from_address(my_var_address).value

ctypes 将变量的地址作为参数。
sys.getRefCount 相比,使用 ctypes 的优点是您无需从结果中减去 1。

import ctypes

my_var = 'hello python'
my_var_address = id(my_var)

ctypes.c_long.from_address(my_var_address).value

ctypes takes address of the variable as an argument.
The advantage of using ctypes over sys.getRefCount is that you need not subtract 1 from the result.

凡尘雨 2024-07-19 23:26:49

Python 中的每个对象都有一个引用计数和一个指向类型的指针。
我们可以使用sys模块获取对象的当前引用计数。 您可以使用sys.getrefcount(object)但请记住,将对象传递给 getrefcount() 会使引用计数增加 1

import sys

name = "Steve"

# 2 references, 1 from the name variable and 1 from getrefcount
sys.getrefcount(name)

Every object in Python has a reference count and a pointer to a type.
We can get the current reference count of an object with the sys module. You can use sys.getrefcount(object), but keep in mind that passing in the object to getrefcount() increases the reference count by 1.

import sys

name = "Steve"

# 2 references, 1 from the name variable and 1 from getrefcount
sys.getrefcount(name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文