我试图了解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
相同?有人可以详细解释吗?非常感谢!
答案 0 :(得分:0)