文章详情页
python 为什么调用函数会令引用计数+2
浏览:219日期:2022-08-05 10:36:08
问题描述
import sysa = 11init_cnt = sys.getrefcount(a) - 1print 'init', init_cnt####init 22def function(c): print ’in function, count: ’, sys.getrefcount(c) - 1 ####in function, count: 24 print ’函数调用,计数器增加’, sys.getrefcount(c) - 1 - init_cnt ####函数调用,计数器增加 2function(a)
问题解答
回答1:把a当参数传入到function后被引用了两次:
for attr in dir(function): print attr, getattr(function, attr)
可以看到参数a被function这个对象中的两个属性分别引用.
相关文章:
1. python - pip install出现下面图中的报错 什么原因?2. objective-c - 使用axios 通过vuex mutation修改数据 getter第一次获取失败 第二次成功3. 前端 - css自定义字体4. PHP中的$this代表当前的类还是方法?5. angular.js - angularjs scope.$watch取不到input变化的值6. css3动画 - css3 animation初始动画卡顿是怎么回事?7. 一道关于 JavaScript 中 this 的题目的困惑8. android - recyclerview刷新问题9. php - 两个表数据怎么插到一个模板中,并按时间排序?10. mysql - 一个sql的问题
排行榜

网公网安备