某些情况下__del__会调用两次? [英] It is __del__ calling twice for some instances?

查看:109
本文介绍了某些情况下__del__会调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


python在类对象的某些

标识上多次调用__del__是不正确的行为?


简而言之,我将描述一种情况。对不起我的英语。


为了调试目的,我把我的模块全局计数器放入

来计算__init__和__del__来电。


这是一个清晰的示例代码:

--------------------------- ----------------------------

init_cnt = 0

del_cnt = 0


class foo:

def __init __(self):

global init_cnt

init_cnt + = 1

def __del __(自我):

全球del_cnt

del_cnt + = 1


def stat():

print" init_cnt =%d" %init_cnt

print" del_cnt =%d" %del_cnt

print" difference =%d" %init_cnt-del_cnt

-------------------------------------- -----------------

和某个时刻的stat()调用的结果

看起来像这样:


init_cnt = 6233

del_cnt = 6234

差异= -1


某些情况下__del__被叫两次?


提前致谢!

-

GMT更多......

Hello!

It is correct behaviour for python to call __del__ on some
identity of a class object more than once?

In brief I shall describe a situation. Sorry for my english.

For debugin purposes I''m put in my module global counters
for counting __init__ and __del__ calls.

This is a sample code for clearness:
-------------------------------------------------------
init_cnt = 0
del_cnt = 0

class foo:
def __init__(self):
global init_cnt
init_cnt += 1

def __del__(self):
global del_cnt
del_cnt += 1

def stat():
print "init_cnt = %d" % init_cnt
print "del_cnt = %d" % del_cnt
print "difference = %d" % init_cnt-del_cnt
-------------------------------------------------------
And the result of a stat() call in some moment of time
looks like so:

init_cnt = 6233
del_cnt = 6234
difference = -1

It is __del__ called twice for some instance?

Thanks in advance!
--
GMT More Then ...

推荐答案

Max Yuzhakov写道:


MY print" difference =%d" %init_cnt-del_cnt


稍作修正。


print" difference =%d" %(init_cnt-del_cnt)

-

GMT更多然后......
Max Yuzhakov writes:

MY print "difference = %d" % init_cnt-del_cnt

Little correction.

print "difference = %d" % (init_cnt-del_cnt)
--
GMT More Then ...


Max Yuzhakov写道:
Max Yuzhakov wrote:

这是清晰度的示例代码:
This is a sample code for clearness:



代码片段不会创建任何foo( )实例,我能看到的......


< / F>

that code snippet doesn''t create any foo() instances, what I can see...

</F>


Max Yuzhakov写道:
Max Yuzhakov wrote:

python在类对象的某些

标识上多次调用__del__是不正确的行为?
It is correct behaviour for python to call __del__ on some
identity of a class object more than once?



不是你给出的代码作为例子,但在一般情况下

是,Python给出的唯一保证

实例上的__del__方法是,在程序运行期间,它将被称为零,一次或多次。在实践中,有各种情况下不会调用

__del__,但如果你在调用__del __期间
复活对象,它只被调用多次。


stat函数的输出当然也可以由

生成,并在另一个线程中创建和销毁大量的foo对象。如果foo是

在前两个打印语句之间创建和销毁,并且

另一个在评估期间创建并销毁了

最后一个打印语句然后你可以在同一个对象中看到你描述的输出

没有任何多个__del__调用。


你应该发布一个工作代码示例,如果你想要一个更有用的答案,那么会产生你的输出。

Not with the code which you gave as an example, but in the general case
yes, the only guarantee that Python gives about the __del__ method on an
instance is that it will be called zero, one or more than one times during
the run of the program. In practice there are various situations where
__del__ will not be called, but it is only called multiple times if you
resurrect the object during a call to __del__.

The output from your stat function could, of course, also be generated by
creating and destroying lots of foo objects in another thread. If a foo was
both created and destroyed between the first two print statements, and
another one was created and destroyed in the middle of the evaluation of
the last print statement then you could see the output you described
without any multiple __del__ calls in the same object.

You should post a working code sample which generates your output if you
want a more useful answer.


这篇关于某些情况下__del__会调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆