The gist:
1. magic method, surrounded by __ double underscores
Automatically invoked at the appropriate time
2. When creating an object, the constructor must be called
3, del destructor, in the del a object, and will certainly call the destructor
The destructor is called only when the reference count of the object is 0, reclaiming resources
The destructor is called when it is destroyed by python's garbage collector. When an object is not referenced, the garbage collector automatically reclaims the resources and calls the destructor.
#coding=utf-8 ''' Magic method, surrounded by __ double underscores are automatically called at the appropriate time ''' #Construct init, destruct del class Rectangle: def __init__(self,x,y): = x = y print('Tectonic') ''' The del destructor is not called when you del a object. The destructor is only called when the reference count of the object reaches 0, to reclaim resources. The destructor is called when it is destroyed by python's garbage collector. When an object is not referenced, the garbage collector automatically reclaims the resources and calls the destructor. ''' def __del__(self): print('Deconstruction') def getPeri(self): return ( + )*2 def getArea(self): return * if __name__ == '__main__': rect = Rectangle(3,4) # a = () # b = () # print(a,b) rect1 = rect del rect1 # del rect while 1: pass
Additional knowledge: a pitfall to be aware of when initializing Python class member variables with default values
A pitfall to be aware of when initializing Python class member variables with default values
Tags (space-separated): python2.7 python 3.6
Consider the following scenario:
Define class A. Class A contains the member variables l and d, l being an array and d being a dictionary.
Initialize the member variables l and d of A with default arguments in the constructor of class A ;-.
The specific code is as follows:
class A: def __init__(self, l=["name"], d={"key1": "test"}): = l = d
Now, the definition generates multiple instances of A in the main logic function, constructed using the default values of the constructor:
if __name__ == "__main__": a1 = A() a2 = A() print (id(), id()) print (id(), id())
The output is as follows:
python2.7 (56305416L, 56376040L) (56305416L, 56376040L) python3.6 2036953558112 2036953558112
It can be seen that the corresponding member variables l and d point to the same address in the two instances of A that were initialized with the default value
Now suppose you need to manipulate instances a1 and a2 separately in the main logic function:
if __name__ == "__main__": a1 = A() a2 = A() # print (id(), id()) # print (id(), id()) (["a", "b", "C", "Xa"]) ["key"] = "value" print ("a1", , ) print ("a2", , )
The output will be as follows:
a1 ['name', 'a', 'b', 'C', 'Xa'] {'key1': 'test', 'key': 'value'}
a2 ['name', 'a', 'b', 'C', 'Xa'] {'key1': 'test', 'key': 'value'}
Only a1 is modified, but the member variables of a2 are changed at the same time!
One of the actual scenarios for this issue is when using wxGride:
class MyGrid(): def __init__(self, parent, col_titles=["a", "b", "c"], data=[["1", "2", "3"]]): .Grid__init__(self, parent=parent) self.col_titls = col_titles = data ... def AppendData(self, rows=[], clear=Flase): (rows) msg = (self, .GRIDTABLE_NOTIFY_ROWS_DELETED, 0, len(rows)) (msg) class MyFrame(): def __init(self, parent, title=""): .__init__(self, parent=parent, title=title) self.grid1 = MyGrid(self) self.grid2 = MyGrid(self) ... def onGridAddCallback(rows, force=False): if isinstance(rows, list) and len(rows) > 0: self.(rows, force)
When the contents of gird1 were updated, the member variable data of gird2 was also changed, resulting in the exception
Optional solutions: Avoid initializing pointer type member variables (list, dict ...) with default values.
class MyFrame(): def __init(self, parent, title=""): .__init__(self, parent=parent, title=title) self.grid1 = MyGrid(self, col_titles=["a", "b", "c"], data=[["1", "2", "3"]]) self.grid2 = MyGrid(self, col_titles=["a", "b", "c"], data=[["1", "2", "3"]]) ...
The above this talk about python3 constructor and destructor is all I have to share with you, I hope it can give you a reference, and I hope you will support me more.