Check the behavior of destructor in Python
Sample code * class_test.py *
class_test.py
class SampleClass:
num = 0
def __init__(self, number=1):
SampleClass.num +=1 #Increment the class variable num by 1 each time an instance is created
self.num = number #Store the argument number received from the generator at the time of instance creation in the instance variable num.
def __del__(self):
SampleClass.num -=1 #Decrement 1 class variable num each time you delete an instance
print("I deleted this instance")
Start Python3 in the same hierarchy as the directory containing the above script file
Terminal
$ ls
class_test.py
$ cat class_test.py
class SampleClass:
num = 0
def __init__(self, number=1):
SampleClass.num +=1 #Increment the class variable num by 1 each time an instance is created
self.num = number #Store the argument number received from the generator at the time of instance creation in the instance variable num.
def __del__(self):
SampleClass.num -=1 #Decrement 1 class variable num each time you delete an instance
print("I deleted this instance")
$
$ python3
>>>
* Import * the * class_test * module to create and delete an instance of the * SampleClass * class.
Terminal
>>> import class_test as ct
>>>
>>> sample_1 = ct.SampleClass()
>>> print(ct.SampleClass.num)
1
>>>
>>> print(sample_1.num)
1
>>>
(Points here)
- Created one instance of * SampleClass * class.
- The value of the class variable * num * that manages the number of instances is displayed as 1.
- In addition, the value of the instance variable * num * of the generated instance * sample_1 * is set to 1 set in the default argument of the constructor.
Terminal
>>> sample_2 = ct.SampleClass(number=4)
>>> print(ct.SampleClass.num)
2
>>> print(sample_2.num)
4
>>>
(Points here)
- I have created another instance of the * SampleClass * class.
- The value of the class variable * num * that manages the number of instances is displayed as 2.
- In addition, the value of the instance variable * num * of the generated instance * sample_2 * is set to 4 passed as an argument to the constructor when * sample_2 * is generated.
Terminal
>>> sample_3 = ct.SampleClass(number=15)
>>> print(ct.SampleClass.num)
3
>>> print(sample_3.num)
15
>>>
(Points here)
- Created another instance of the * SampleClass * class. You have now created a total of 3 instances.
- The value of the class variable * num * that manages the number of instances is displayed as 3.
- In addition, the value of the instance variable * num * that the generated instance * sample_3 * has is set to 15 that was passed as an argument to the constructor when * sample_3 * was generated.
From here, you can check the behavior of the destructor.
Terminal
>>> del sample_2
I deleted this instance
>>>
(Points here)
-
- print ("This instance has been deleted") described in * destructor * of * SampleClass * class has been executed.
- You now have two instances.
Terminal
>>> print(ct.SampleClass.num)
2
>>> print(sample_1.num)
1
>>> print(sample_3.num)
15
>>> print(sample_2.num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sample_2' is not defined
>>>
(Points here)
- The value of the class variable * num * that manages the number of instances is displayed as 2.
- When you delete the instance named * sample_2 *, you can see that the process of decrementing the class variable * num * described in * destructor * by 1 was executed properly.
- At this time, it was also confirmed that the instance variables of the two instances (* sample_1 * and * sample_3 *) other than the deleted instance did not change in value and were not unintentionally affected. ..
(Addition)
The code will be shorter if the * import * statement is as follows.
import statement (after change)
from class_test import SampleClass
Terminal
>>> from class_test import SampleClass
>>>
>>> sample_1 = SampleClass()
>>> print(SampleClass.num)
1
>>> print(sample_1.num)
1
>>>
>>> sample_2 = SampleClass(number=4)
>>> print(SampleClass.num)
2
>>> print(sample_2.num)
4
>>>
(Reference web page)
- How to use Python destructor [for beginners]
- [Python] Find the number of instances
- About Python classes
- @ ysk24ok's Qiita article "[Python] Import Stumbling Point"