__del__ 方法在 python 中被意外调用

发布于 2024-08-15 09:44:11 字数 1466 浏览 3 评论 0原文

我是 python 新手,一直在研究 Swaroop CH 的“A Byte of Python”中的示例。我看到 __del__ 方法的一些行为令我困惑。

运行以下脚本(在 Python 2.6.2 中),

class Person4:
    '''Represents a person'''
    population = 0

    def __init__(self, name):
        '''Initialize the person's data'''
        self.name = name
        print 'Initializing %s'% self.name

        #When the person is created they increase the population
        Person4.population += 1

    def __del__(self):
        '''I am dying'''
        print '%s says bye' % self.name

        Person4.population -= 1

        if Person4.population == 0:
            print 'I am the last one'
        else:
            print 'There are still %d left' % Person4.population


swaroop = Person4('Swaroop')
kaleem = Person4('Kalem')

基本上,如果我使用 Python 控制台(或 Spyder 交互式控制台)

我会看到以下内容: <块引用> <块引用>

execfile(u'C:\1_eric\Python\test1.py')
初始化 Swaroop
初始化卡勒姆

execfile(u'C:\1_eric\Python\test1.py')
初始化 Swaroop
斯瓦鲁普说再见
我是最后一个
初始化 Kalem
卡勒姆说再见
我是最后一个

为什么在第二次运行时会在 __init__ 之后立即调用 __del__ 方法?
我猜测,由于使用了相同的实例名称(“swaroop”和“kaleem”),因此它正在释放原始实例并对其进行垃圾收集。但是,这似乎对当前的人口数量造成了严重破坏。

这是怎么回事?
避免这种混乱的好方法是什么?
避免使用__del__? 在重用之前检查现有实例名称? ...

谢谢, 埃里克

I am new to python and have been working through the examples in Swaroop CH's "A Byte of Python". I am seeing some behavior with the __del__ method that is puzzling me.

Basically, if I run the following script (in Python 2.6.2)

class Person4:
    '''Represents a person'''
    population = 0

    def __init__(self, name):
        '''Initialize the person's data'''
        self.name = name
        print 'Initializing %s'% self.name

        #When the person is created they increase the population
        Person4.population += 1

    def __del__(self):
        '''I am dying'''
        print '%s says bye' % self.name

        Person4.population -= 1

        if Person4.population == 0:
            print 'I am the last one'
        else:
            print 'There are still %d left' % Person4.population


swaroop = Person4('Swaroop')
kaleem = Person4('Kalem')

using the Python console (or the Spyder interactive console) I see the following:

execfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Initializing Kalem

execfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Swaroop says bye
I am the last one
Initializing Kalem
Kalem says bye
I am the last one

Why is the __del__ method being called immediately after the __init__ on the second run?
I am guessing that since the same instance names ('swaroop' and 'kaleem') are being used that it is releasing the original instance and garbage collecting it. But, this seems to be playing havoc with the current population count.

What is going on here?
What is a good way to avoid this sort of confusion?
Avoid the use of __del__?
Check for existing instance names before reusing them?
...

Thanks,
Eric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め可乐爱微笑 2024-08-22 09:44:11

这里发生了一些事情。当您的 Person4 类实例化时,它会将其 population 类变量初始化为 0。在交互式控制台中,您似乎多次运行“test1.py”文件。第二次运行它时,会再次声明 Person4 类,这使得它在技术上与第一次不同(即使它具有相同的名称)。这意味着它有自己独立的人口计数。

现在,swaroopkaleem全局变量,在“test1.py”的两个实例之间共享。 Python 在内部使用引用计数来进行大部分自动垃圾回收,因此第一个 Person4 类的原始实例直到第二次分配给 swaroop 时才会被释放。分配给 swaroop 会减少第一个实例的引用计数,从而导致调用 __del__ ,因为引用计数现在为零。但因为您在 __del__() 中通过名称引用 Person4,所以当前一个实例消失时,它会减少实例em> Person4.population 计数,而不是旧的 Person4 人口计数。

希望这是有道理的。我明白为什么这可能会让学习 Python 的人感到困惑。在使用 execfile() 重新定义 Person4 类的同时使用类变量会进一步造成混乱。无论如何,我已经编写了很多 Python 代码,并且我认为我不需要使用 __del__ 特殊方法。

There are a couple of things going on here. When your Person4 class is instantiated, it initialises its population class variable to 0. From your interactive console, you appear to be running your "test1.py" file multiple times. The second time you run it, the Person4 class is declared again which makes it technically different from the first one (even though it has the same name). That means it has its own independent population count.

Now, swaroop and kaleem are global variables, shared between both your instances of "test1.py". Python internally uses reference counting for most of its automatic garbage collection, so the original instance of the first Person4 class is not released until the second assignment to swaroop. Assigning to swaroop decrements the reference count for the first instance, causing __del__ to be called because the reference count is now zero. But because you're referring to Person4 by name inside __del__(), when the previous instance disappears it decrements the new Person4.population count, instead of the old Person4 population count.

Hopefully that made sense. I can see why this might be confusing to somebody learning Python. Your use of class variables at the same time as redefining the Person4 class using execfile() is further confusing matters. For what it's worth, I've written a lot of Python code and I don't think I've ever needed to use the __del__ special method.

巴黎盛开的樱花 2024-08-22 09:44:11

一般建议:不要在 Python 中使用 __ del __。它可以通过多种方式破坏垃圾收集,特别是。在对象之间循环引用的情况下。

在您的示例中,存在与 execfile() 的使用(这不是最佳实践)以及全局变量的重新定义相关的各种问题。顺便说一句,如果您确实需要创建一个伪析构函数(即每当对象被垃圾收集时调用的代码),请编写一个所谓的“终结器”函数(它不是一个正确的析构函数)并使用weakref调用它.ref 回调。当然,它不应该是一个实例方法,并且记住 lambda 实际上创建了一个闭包,因此请确保不要在回调中泄漏对 self 的任何引用!如果您需要来自被销毁实例的数据,请使用 func 默认参数方法,只需确保从不在 lambda 内引用“self”,否则它将无法工作。

from weakref import ref
from time import sleep

class Person4:
    '''Represents a person'''
    population = 0

    def __init__(self, name):
        '''Initialize the person's data'''
        self.name = name
        print 'Initializing %s'% self.name

        #When the person is created they increase the population
        Person4.population += 1

        self._wr = ref(self, lambda wr, name=self.name: Person4_finalizer(name))

def Person4_finalizer(name):
        '''I am dying'''
        print '%s says bye' % name

        Person4.population -= 1

        if Person4.population == 0:
            print 'I am the last one'
        else:
            print 'There are still %d left' % Person4.population

p1 = Person4("one")
p2 = Person4("two")
p3 = Person4("three")

del p2
del p3
sleep(5)

输出(睡眠是为了帮助了解发生了什么):

Initializing one
Initializing two
Initializing three
two says bye
There are still 2 left
three says bye
There are still 1 left
one says bye
I am the last one

General advice: don't use __ del __ in Python. It can break garbage collection in a number of ways, esp. in the case of circular references between objects.

In your example, there're various issues related to the usage of execfile() - which is not a best practice - and the redefinition of global variables. By the way, if you really need to create a pseudo-destructor (i.e. a code that is invoked whenever the object gets garbage collected), write a so-called "finalizer" function (it's not properly a destructor) and invoke it using weakref.ref callback. It should NOT be an instance method of course, and remember that lambda actually creates a closure, hence be sure not to leak any reference to self in the callback! If you need data from the destroyed instance, use the func default argument approach, just be sure never to reference 'self' inside the lambda, otherwise it won't work.

from weakref import ref
from time import sleep

class Person4:
    '''Represents a person'''
    population = 0

    def __init__(self, name):
        '''Initialize the person's data'''
        self.name = name
        print 'Initializing %s'% self.name

        #When the person is created they increase the population
        Person4.population += 1

        self._wr = ref(self, lambda wr, name=self.name: Person4_finalizer(name))

def Person4_finalizer(name):
        '''I am dying'''
        print '%s says bye' % name

        Person4.population -= 1

        if Person4.population == 0:
            print 'I am the last one'
        else:
            print 'There are still %d left' % Person4.population

p1 = Person4("one")
p2 = Person4("two")
p3 = Person4("three")

del p2
del p3
sleep(5)

output (the sleep is there to help see what's happening):

Initializing one
Initializing two
Initializing three
two says bye
There are still 2 left
three says bye
There are still 1 left
one says bye
I am the last one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文