Jon*_*han 7 python garbage-collection
是否Python解释器会优雅的处理,其中一个对象实例删除最后一个引用自身的情况?
考虑以下(无可置疑的无用)模块:
all_instances = []
class A(object):
def __init__(self):
global all_instances
all_instances.append(self)
def delete_me(self):
global all_instances
self.context = "I'm still here"
all_instances.remove(self)
print self.context
Run Code Online (Sandbox Code Playgroud)
现在用法:
import the_module
a = the_module.A()
the_deletion_func = a.delete_me
del a
the_deletion_func()
Run Code Online (Sandbox Code Playgroud)
这仍然会打印I'm still here
,但是Python的垃圾收集器是否存在竞争条件,即将收集对象实例?
对象功能的引用是否节省了一天?
解释器是否保留对当前正在执行其代码的对象的引用,直到它完成为止?
不,没有任何这样的竞争条件.您正在清除引用,因此引用计数降为1,删除方法引用后将清除对象.
该the_deletion_func
引用指向一个方法,该方法指向实例(以及类),因此仍然存在引用.
当前正在执行的方法有一个局部变量self
,它也是对实例的引用,但主要是提供该引用的方法包装器.
归档时间: |
|
查看次数: |
105 次 |
最近记录: |