2024-09-30 06:18:03 发布
网友
在我运行“a.method”之后,为什么系统getrefcount(a) 返回3?没有引用对象的新变量
class A(object): def method(): pass import sys a=A() sys.getrefcount(a) # returns 2 a.method <bound method A.method of <__main__.A object at 0x7f1e73059b50>> sys.getrefcount(a) # returns 3
在python交互式shell中,最后一个命令的结果存储在a special varialbe named ^{}中。当然,这个变量包含了对结果的引用。你知道吗
在您的例子中,结果是一个method对象,其中包含对其“self”的引用,即变量a。换句话说,在你描述的情况下,额外的ref是间接的。由于变量_而保持活动状态的结果(<bound method A.method of <__main__.A object at 0x7f1e73059b50>>)包含对<__main__.A object at 0x7f1e73059b50>的引用。你知道吗
a
_
<bound method A.method of <__main__.A object at 0x7f1e73059b50>>
<__main__.A object at 0x7f1e73059b50>
在python交互式shell中,最后一个命令的结果存储在a special varialbe named ^{} 中。当然,这个变量包含了对结果的引用。你知道吗
在您的例子中,结果是一个method对象,其中包含对其“self”的引用,即变量
a
。换句话说,在你描述的情况下,额外的ref是间接的。由于变量_
而保持活动状态的结果(<bound method A.method of <__main__.A object at 0x7f1e73059b50>>
)包含对<__main__.A object at 0x7f1e73059b50>
的引用。你知道吗相关问题 更多 >
编程相关推荐