我有一个关于对象销毁和在程序中间调用析构函数的查询
例:
class ABC:
# Initializing
def __init__(self):
print('Constructor Called')
# Destructor
def __del__(self):
print('Destructor called')
obj = ABC()
del obj
# I want destructor to be called here.
# and then print below statement
print("obj deleted")
但在程序结束时调用析构函数
有什么帮助吗,德夫
当对象的引用计数达到0时,或者在循环引用的情况下由垃圾收集器遍历时,调用
__del__
方法。例如,以下各项:将正确调用
__del__
方法并打印Foo has been deleted!
,因为分配给foo
的对象只被引用一次。现在我们来看看下面的例子:无法调用
__del__
方法,因为foo.self
将自身作为人质。只有当垃圾收集器遍历它时,才会将其删除。例如:将调用
__del__
方法,因为我们告诉收集器遍历挂起的对象。不建议这样做,但您可以通过将阈值设置为1来告诉Python遍历所有队列。例如:因此,每次实例化一个对象时,收集器都会执行其运行。但同样,不建议这样做,因为这样做成本高昂,并且会影响代码的性能
有关详细信息,请参见
__del__()
(here)的文档:如果您试图“解构”一个Python对象(注意我在引号中是怎么说的),我建议您使用
__exit__()
和with
语句(docshere)要使用
with
语句,请使用以下方法定义一个类:下面是一个简单的例子:
尽管坦率地说,在Python中“解构”一个对象几乎是不需要的,而且在大多数情况下,编程时应该避免尝试执行garbage collector的工作。使用
del
是一回事,但尝试“解构”对象是另一回事相关问题 更多 >
编程相关推荐