【发布时间】:2019-08-17 18:49:17
【问题描述】:
我正在尝试了解 Python 中的引用计数。这是我从帖子 (https://rushter.com/blog/python-garbage-collector/) 中得到的一个示例:
foo = []
# 2 references, 1 from the foo var and 1 from getrefcount
print(sys.getrefcount(foo))
def bar(a):
# 4 references
# from the foo var, function argument, getrefcount and Python's function stack
print(sys.getrefcount(a))
bar(foo)
# 2 references, the function scope is destroyed
print(sys.getrefcount(foo))
我不清楚为什么第二个sys.getrefCount 是4。作者说这四个引用来自foo var、函数参数、getrefcount 和Python 的函数堆栈。调用bar(foo) 的引用不是和Python's function stack 一样吗?有人可以详细解释一下吗?非常感谢!
【问题讨论】: