python 为什么调用函数会令引用计数+2
黄舟
黄舟 2017-04-18 10:32:22
0
1
543
import sys
a = 11
init_cnt = sys.getrefcount(a) - 1

print "init", init_cnt
####init 22
def function(c):
    print 'in function, count: ', sys.getrefcount(c) - 1
    ####in function, count:  24
    print '函数调用,计数器增加', sys.getrefcount(c) - 1 - init_cnt
    ####函数调用,计数器增加 2
function(a)

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
Peter_Zhu

After passing a as a parameter to function, it is referenced twice:

for attr in dir(function):
    print attr, getattr(function, attr)

You can see that parameter a is referenced by two attributes in the function object.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!