Python - 析构函数不删除对象实例
Python - deconstructor does not delete object instance
我有以下问题,我是 Python 脚本的业余用户。我创建了以下 class
roof_height = 3.5 #m
E = 30000*(10^3) #kN/m^2
class Brace():
_registry = []
def __init__(self,name,coord,b,d, h = roof_height):
print('Brace created.')
Brace._registry.append(self)
self.name = name
self.coord = coord # pass tuple in the form of (x,y)
self.b = b # //x global
self.d = d # //y global
self.h = h
def __del__(self):
print('Destructor called, Brace deleted.')
Brace._registry.remove(self)
def properties(self):
print("properties of " + self.name + " calculated")
As = self.b*self.d
Iy = self.b*(self.d**3)*(1/12)
Iz = (self.b**3)*(self.d)*(1/12)
self.Iy = Iy
self.Iz = Iz
self.As = As
def stiffness(self):
print("stiffnesses of " + self.name + " calculated")
Kx = 12*E*self.Iy/self.h
Ky = 12*E*self.Iz/self.h
self.Kx = Kx
self.Ky = Ky
我已经创建了 class 的 _registry general 属性,因此我可以遍历对象,效果很好。我面临的问题如下:
1. 当我创建一个对象时打印出
'Brace created'
显示,但是当我删除它时消息
'Destructor called, Brace deleted.'
删除对象时不显示,而且_registry列表在不删除对象的情况下保持不变。列表的长度显然保持不变。
- 如何避免使用相同的名称创建相同的实例两次,我的意思是如何检查重复的实例(相同的对象名称和属性值)
我必须为它编写代码还是有我可以使用的内置函数?
1:__del__() 可能由于与垃圾收集有关的多种原因而未被调用。来自 documentation:
Note del x doesn’t directly call x.__del__() — the former decrements
the reference count for x by one, and the latter is only called when
x‘s reference count reaches zero. Some common situations that may
prevent the reference count of an object from going to zero include:
circular references between objects (e.g., a doubly-linked list or a
tree data structure with parent and child pointers); a reference to
the object on the stack frame of a function that caught an exception
(the traceback stored in sys.exc_info()[2] keeps the stack frame
alive); or a reference to the object on the stack frame that raised an
unhandled exception in interactive mode (the traceback stored in
sys.last_traceback keeps the stack frame alive). The first situation
can only be remedied by explicitly breaking the cycles; the second can
be resolved by freeing the reference to the traceback object when it
is no longer useful, and the third can be resolved by storing None in
sys.last_traceback. Circular references which are garbage are detected
and cleaned up when the cyclic garbage collector is enabled (it’s on
by default). Refer to the documentation for the gc module for more
information about this topic.
与其依赖内置的内存管理功能,也许您可以实现一个定制的功能,以便在不再需要某个对象时释放资源?
2:要避免有两个同名的实例,请使用 factory pattern.
我有以下问题,我是 Python 脚本的业余用户。我创建了以下 class
roof_height = 3.5 #m
E = 30000*(10^3) #kN/m^2
class Brace():
_registry = []
def __init__(self,name,coord,b,d, h = roof_height):
print('Brace created.')
Brace._registry.append(self)
self.name = name
self.coord = coord # pass tuple in the form of (x,y)
self.b = b # //x global
self.d = d # //y global
self.h = h
def __del__(self):
print('Destructor called, Brace deleted.')
Brace._registry.remove(self)
def properties(self):
print("properties of " + self.name + " calculated")
As = self.b*self.d
Iy = self.b*(self.d**3)*(1/12)
Iz = (self.b**3)*(self.d)*(1/12)
self.Iy = Iy
self.Iz = Iz
self.As = As
def stiffness(self):
print("stiffnesses of " + self.name + " calculated")
Kx = 12*E*self.Iy/self.h
Ky = 12*E*self.Iz/self.h
self.Kx = Kx
self.Ky = Ky
我已经创建了 class 的 _registry general 属性,因此我可以遍历对象,效果很好。我面临的问题如下: 1. 当我创建一个对象时打印出
'Brace created'
显示,但是当我删除它时消息
'Destructor called, Brace deleted.'
删除对象时不显示,而且_registry列表在不删除对象的情况下保持不变。列表的长度显然保持不变。
- 如何避免使用相同的名称创建相同的实例两次,我的意思是如何检查重复的实例(相同的对象名称和属性值) 我必须为它编写代码还是有我可以使用的内置函数?
1:__del__() 可能由于与垃圾收集有关的多种原因而未被调用。来自 documentation:
Note del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero. Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in sys.exc_info()[2] keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in sys.last_traceback keeps the stack frame alive). The first situation can only be remedied by explicitly breaking the cycles; the second can be resolved by freeing the reference to the traceback object when it is no longer useful, and the third can be resolved by storing None in sys.last_traceback. Circular references which are garbage are detected and cleaned up when the cyclic garbage collector is enabled (it’s on by default). Refer to the documentation for the gc module for more information about this topic.
与其依赖内置的内存管理功能,也许您可以实现一个定制的功能,以便在不再需要某个对象时释放资源?
2:要避免有两个同名的实例,请使用 factory pattern.