文章詳情頁
python 為什么調用函數會令引用計數+2
瀏覽:91日期: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. ruby - gitlab托管,git clone 失敗?2. javascript - node.js promise沒用3. android 如何實現如圖中的鍵盤上的公式及edittext的內容展示呢4. javascript - js 寫一個正則 提取文本中的數據5. 算法 - python 給定一個正整數a和一個包含任意個正整數的 列表 b,求所有<=a 的加法組合6. golang - 用IDE看docker源碼時的小問題7. yii2中restful配置好后在nginx下報404錯誤8. c++ - 如何正確的使用QWebEngineView?9. java - 我在用Struts2上傳文件時,報以下錯誤怎么回事?10. 網站被黑,請教下大神,怎么對datebase.php內容加密。
排行榜
