关于系统getrefcoun

2024-09-30 06:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在我运行“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

Tags: of对象importobjectmain系统defsys
1条回答
网友
1楼 · 发布于 2024-09-30 06:18:03

在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>的引用。你知道吗

相关问题 更多 >

    热门问题