0
我正在浏览本网站http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python,我写了完全相似的代码。 但是在我的代码中,一旦对象超出范围,destructor
就会被调用。但是在上面的链接中提到的代码destructor
在代码结束后被调用。怎么样?按照Python中析构函数的调用顺序混淆
这里是代码;从链接
代码
class FooType(object):
def __init__(self, id):
self.id = id
print self.id, 'born'
def __del__(self):
print self.id, 'died'
def make_foo():
print 'Making...'
ft = FooType(1)
print 'Returning...'
return ft
print 'Calling...'
ft = make_foo()
print 'End...'
Output is :
Calling...
Making...
1 born
Returning...
End...
1 died <----- Destructor called
我的代码:
abc = [1,2,3]
class myclass(object):
def __init__(self):
print "const"
abc = [7,8,9]
a = 4
def __del__(self):
print "Dest"
def hello():
abc = [4,5]
print abc
my = myclass()
print my.abc, my.a
print "I am before Dest"
return "Done"
ret = hello()
print ret
print abc
输出:
[4, 5]
const
[7, 8, 9] 4
I am before Dest
Dest<---------- Destructor
Done
[1, 2, 3]
由于程序不会在您写入的最后一行执行时立即结束,因此仍有一些整理工作要做(例如,您的对象被解除引用和'__del__'eted)。 – jonrsharpe