時間:2022-09-12來源:www.farandoo.com作者:電腦系統城
_init__
函數是python 類的構造函數,在創建一個類對象的時候,就會自動調用該函數;可以用來在創建對象的時候,設置該對象的一些初始化信息和設置。__del__
函數是python 類的析構函數,在一個類對象生命周期結束、被銷毀的時候,就會自動調用該函數;主要用來釋放對象占用的一些資源等。如下,編寫了一個 demo 類的實現代碼。
1 2 3 4 5 6 7 8 9 |
>>> class demo(): ... def __init__( self ): ... print ( "init class" ) ... print ( self ) ... def __del__( self ): ... print ( "del class" ) ... print ( self ) ... >>> |
該類對象在創建的時候,會調用 __init__
函數,打印出 “init class”;
該類對象在銷毀的時候,會調用 __del__
函數,打印出 “del class”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> 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
”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
>>> 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 對象立即被釋放。
1 2 3 |
>>> def return_demo(): ... return demo() ... |
定義函數 return_demo,該函數內部創建類對象,并且返回創建出的類對象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
>>> 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
。
第二次執行相同的操作,可以看到現象相同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
>>> 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
>>> 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 >>> |
到此這篇關于python 類對象的析構釋放代碼演示的文章就介紹到這了,
2022-09-12
Python遺傳算法Geatpy工具箱使用介紹2022-09-12
Python實現K-近鄰算法的示例代碼2022-09-12
Flask框架debug與配置項的開啟與設置詳解很多的編程工作者們應該都會使用到python,昨天我在用python的時候看到很多的代碼里面都會有range()這個函數,但是不知道這個到底是什么意思,也去網上查了一下,下文小編就給大家總結了python中range()函數的用法以及其意思...
2022-06-25