網站首頁 編程語言 正文
一、類的構造函數與析構函數
-
_init__
函數是python 類的構造函數,在創建一個類對象的時候,就會自動調用該函數;可以用來在創建對象的時候,設置該對象的一些初始化信息和設置。 -
__del__
函數是python 類的析構函數,在一個類對象生命周期結束、被銷毀的時候,就會自動調用該函數;主要用來釋放對象占用的一些資源等。
二、代碼演示
1. 引用的更迭
如下,編寫了一個 demo 類的實現代碼。
>>> class demo(): ... def __init__(self): ... print("init class") ... print(self) ... def __del__(self): ... print("del class") ... print(self) ... >>>
該類對象在創建的時候,會調用 __init__
函數,打印出 “init class”;
該類對象在銷毀的時候,會調用 __del__
函數,打印出 “del class”。
>>> a1 = demo() init class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> a2 = demo() init class <__main__.demo instance at 0x7f328f7c6d40> >>> >>> >>> >>> a1 = demo() init class <__main__.demo instance at 0x7f328f7c6d88> del class <__main__.demo instance at 0x7f328f7c6cb0> >>>
首先使用變量 a1 引用一個 demo 類對象,此時打印出"init class",以及a1 變量所引用的對象地址 0x7f328f7c6cb0
;
使用變量 a2 引用另外的一個 demo 類對象,此時打印出"init class",以及a2 變量所引用的對象地址 0x7f328f7c6d40
;
a1 和 a2 變量所引用的類對象是不同的兩個對象 0x7f328f7c6cb0
和 0x7f328f7c6d40
。
最后創建一個 demo 類對象,再次使用 a1 變量來指向,此時 a1 引用了新的類對象,引用地址為 0x7f328f7c6d88
;同時,由于之前 a1 引用的對象0x7f328f7c6cb0
不再有人引用它,因此舊的 demo 類對象的空間被釋放,打印出了 “del class 0x7f328f7c6cb0
”。
2. 只在函數內部的類對象
>>> def create_demo(): ... inst = demo() ... >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> >>> >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> >>> >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>>
定義一個函數 create_demo,該函數的作用是創建一個 demo 類對象,并且使用 inst 變量來引用創建的類對象。
調用一次 create_demo() 函數,可以看到,demo 對象被創建,地址為 0x7f328f7c6cb0
;接著該 demo 對象立即被釋放;因為該對象只在 create_demo 函數范圍內有效,函數結束,demo 對象就被回收釋放。
再調用一次 create_demo() 函數,現象相同:demo 對象被創建,地址為 0x7f328f7c6cb0
;接著該 demo 對象立即被釋放。
三、函數內部返回的類對象
>>> def return_demo(): ... return demo() ...
定義函數 return_demo,該函數內部創建類對象,并且返回創建出的類對象。
>>> True True >>> return_demo() init class <__main__.demo instance at 0x7fc511eb8cb0> <__main__.demo instance at 0x7fc511eb8cb0> >>> >>> True del class <__main__.demo instance at 0x7fc511eb8cb0> True >>> >>> return_demo() init class <__main__.demo instance at 0x7fc511eb8cb0> <__main__.demo instance at 0x7fc511eb8cb0> >>> >>> >>> >>> True del class <__main__.demo instance at 0x7fc511eb8cb0> True >>> >>>
可以看到,第一次調用函數 return_demo(),打印的內容顯示,此時創建了一個對象,對象地址為 0x7fc511eb8cb0
;函數 return_demo 內部使用 return
語句返回創建的類對象,因此函數返回時,不會釋放對象 0x7fc511eb8cb0
。
接著,執行一條 Python 語句:True
,同時看到對象 0x7fc511eb8cb0
被釋放。因為程序執行完 return_demo() 函數之后,發現后面的程序并沒有引用 return_demo() 返回的對象,因此 Python 便會釋放對象空間 0x7fc511eb8cb0
。
第二次執行相同的操作,可以看到現象相同。
>>> v1_demo = None >>> v2_demo = None >>> print(v1_demo) None >>> print(v2_demo) None >>> True True >>> >>> v1_demo = return_demo() init class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(v1_demo) <__main__.demo instance at 0x7fc511eb8d88> >>> >>> True True >>> >>> >>> v2_demo = return_demo() init class <__main__.demo instance at 0x7fc511eb8dd0> >>> >>> print(v2_demo) <__main__.demo instance at 0x7fc511eb8dd0> >>> True True >>> >>> >>> >>> >>> v1_demo = None del class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(v1_demo) None >>>
該代碼段的現象和上個代碼段的現象基本相同。
可以看到,v1_demo 和 v2_demo 引用了類對象,所以 v1_demo 和 v2_demo 的值不再是 None
。
最后,我們讓 v1_demo 重新為 None
。此時,v1_demo 引用的對象 0x7fc511eb8d88
就被釋放了。
1. 使用全局變量 引用 函數內部的類對象
>>> g_demo = None >>> print(g_demo) None >>> >>> def return_gdemo(): ... global g_demo ... g_demo = demo() ... >>> >>> print(g_demo) None >>> return_gdemo() init class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(g_demo) <__main__.demo instance at 0x7fc511eb8d88> >>> >>> True True >>>
原文鏈接:https://blog.csdn.net/weixin_42109053/article/details/126733120
相關推薦
- 2022-04-06 Python?numpy視圖與副本_python
- 2022-04-25 Oracle導出導入表結構操作實戰記錄_oracle
- 2023-01-20 Flask框架使用異常捕獲問題_python
- 2022-12-12 Flutter手機權限檢查與申請實現方法詳解_Android
- 2022-07-09 JQuery中this的指向詳解_jquery
- 2022-08-21 Golang中slice刪除元素的性能對比_Golang
- 2022-11-07 go?分布式鎖簡單實現實例詳解_Golang
- 2022-08-17 python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支