文章目錄
- 一、類的構造函數與析構函數
-
- 二、代碼示範
-
-
-
- 1. 引用的更疊
- 2. 隻在函數内部的類對象
-
- 三、函數内部傳回的類對象
-
-
- 1. 使用全局變量 引用 函數内部的類對象
-
-
一、類的構造函數與析構函數
- 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
>>>