一个类内的生成器阻止__del__ ?? [英] Generator inside a class prevent __del__ ??
问题描述
我遇到了这个问题,并且找不到任何解决方案(python 2.2.2)
:
代码:
===========
来自__future__导入生成器的
class titi:
def __init __(self):
print" init"
def __del __(自我):
print" del"
def Gen(个体经营):
收益率1
c = titi()
init c = []
del
==============
这里,一切都很正常......
但是要创建一个发电机:
代码:
===== ======
class toto:
def __init __(self):
print" init"
self。 Coroutine = self.Gen()
def __del __(自我):
print" del"
def Gen(self):
收益率1
a = toto()
init c = []
< ---没有什么!!!
==============
我无法理解为什么在创建生成器时没有调用析构函数,以及我应该怎样做才能得到正确的结果。行为。
(也许我错过了一些明显的东西,但我找不到它)
感谢您的帮助,
Emmanuel
" Emmanuel" < EA ***** @ free.fr>在消息中写道
news:40 *************** @ free.fr ...我遇到了这个问题,并且找不到任何解决方案(python 2.2.2)
....这里,一切都很正常......
但创建一个发电机:
你们都定义了生成器功能并调用它来创建生成器
iterator。
代码:
===========
class toto:def __init __(self):
print" init"
self.Coroutine = self.Gen()
这会创建一个参考循环。删除这个(并在下面更正错误)和
''问题''将消失。
def __del __(self):
print" del"
def Gen(self):
如果你在生成的迭代器中没有真正使用self,那么在类之外定义这个
而不用self作为参数和问题将会消失。
收益率1
a = toto()
你的意思是' 'c = toto()''?
init c = []
< ---没有什么!!! /> ==============
我无法理解为什么在创建生成器时不会调用析构函数,我应该怎么做做一个正确的做法行为。
要么不创建参考循环,要么用del c.Coroutine打破它。
Terry J. Reedy
文章< 40 *************** @ free.fr> ;,Emmanuel< ea ** ***@free.fr>
写道:
class toto:def __init __(self):
print" init"
self.Coroutine = self.Gen()
def __del __(self):
print" del"
def Gen(self):
产量1
a = toto()init c = []< ---没有什么!!!
首先,a仍在引用你的toto对象。我想你
的意思是a = []这里。但即使你做了a = [],析构函数
仍然没有被调用。必须仍然有对该对象的引用。我的b $ b猜测是生成器(直接或间接)引用了
对象,创建了一个自引用循环。
考虑以下仅仅引用函数的修改,
并且不创建生成器:
class tata:
.... def __init__ (个体经营):
.... print" init"
.... self.Coroutine = self.Gen
.. .. def __del __(self):
.... print" del"
.... def Gen(self):
....传递
.... a = tata()
init a = []
这里是如何打破这个循环:
b = tata()
init b.Coroutine = None
b = []
del
-Mark
Mark Day写道:仍然没有被调用。必须仍然有对该对象的引用。我的猜测是生成器(直接或间接)引用了
对象,创建了一个自引用循环。
Python有垃圾收集器将尝试使用
循环引用来查找这些对象。
来自test import *a = toto()
init a =无
导入gc
gc.garbage
[] gc.collect()
4 gc.garbage
[< test.toto实例位于0x81cb78c>]
我检查了文档那个gc.garbage列表并且它说
如果cyles具有带有__del__方法的
对象,则收集器不能在周期中释放对象。所以它把它们放在这个列表中。
我想知道其他垃圾收集者在这种情况下做了什么?有人
知道吗? Java?
Rob
Hi,
I run across this problem, and couldn''t find any solution (python 2.2.2)
:
Code :
===========
from __future__ import generators
class titi: def __init__(self):
print "init"
def __del__(self):
print "del"
def Gen(self):
yield 1
c = titi() init c = [] del
==============
Here, everything is normal...
But creating a generator :
Code :
===========
class toto: def __init__(self):
print "init"
self.Coroutine = self.Gen()
def __del__(self):
print "del"
def Gen(self):
yield 1
a = toto() init c = []
<--- Nothing there !!!
==============
I can''t understand why the destructor is not called when a generator is
created, and what I should do to have a "correct" behavior.
(perhaps I missed something obvious, but I can''t find it )
Thank you for any help,
Emmanuel
"Emmanuel" <ea*****@free.fr> wrote in message
news:40***************@free.fr...I run across this problem, and couldn''t find any solution (python 2.2.2) .... Here, everything is normal...
But creating a generator :
You both defined generator function and called it to create generator
iterator.
Code :
===========class toto: def __init__(self):
print "init"
self.Coroutine = self.Gen()
This creates a reference loop. Delete this (and correct typo below) and
''problem'' will disappear.
def __del__(self):
print "del"
def Gen(self):
If you do not really use self in the resulting iterator, define this
outside of the class without self as a parameter, and problem will
disappear.
yield 1
a = toto()
did you mean ''c = toto()''?
init c = []
<--- Nothing there !!!
==============
I can''t understand why the destructor is not called when a generator is
created, and what I should do to have a "correct" behavior.
Either do not create reference loop or break it with del c.Coroutine.
Terry J. Reedy
In article <40***************@free.fr>, Emmanuel <ea*****@free.fr>
wrote:
class toto: def __init__(self):
print "init"
self.Coroutine = self.Gen()
def __del__(self):
print "del"
def Gen(self):
yield 1
a = toto() init c = [] <--- Nothing there !!!
First of all, "a" is still referencing your toto object. I think you
meant "a = []" here. But even if you did "a = []", the destructor
still isn''t called. There must still be a reference to the object. My
guess is that the generator (directly or indirectly) is referencing the
object, creating a self referential loop.
Consider the following modification that merely references a function,
and does not create a generator:
class tata: .... def __init__(self):
.... print "init"
.... self.Coroutine = self.Gen
.... def __del__(self):
.... print "del"
.... def Gen(self):
.... pass
.... a=tata() init a=[]
Here''s how to break that loop:
b=tata() init b.Coroutine=None
b=[] del
-Mark
Mark Day wrote:still isn''t called. There must still be a reference to the object. My
guess is that the generator (directly or indirectly) is referencing the
object, creating a self referential loop.
Python has a garbage collector that will try to find these objects with
cyclic references.
from test import *a = toto() init a = None
import gc
gc.garbage [] gc.collect() 4 gc.garbage [<test.toto instance at 0x81cb78c>]
I checked out the documentation for that gc.garbage list and it says
that the collector can''t free objects in cycles if the cyles have
objects that have __del__ methods. So it puts them in this list.
I wonder what other garbage collectors do in this situation? Anyone
know? Java?
Rob
这篇关于一个类内的生成器阻止__del__ ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!