简体   繁体   English

为什么Python在对象之前销毁类变量?

[英]Why does Python destroy class variables before objects?

I understand that Python doesn't guarantee the order of destruction of objects at the end of the program, or even that it will happen. 我知道Python不保证在程序结束时破坏对象的顺序,甚至不会保证它会发生。

So I understand that a class's destructor can't rely on global variables, including other modules. 所以我理解一个类的析构函数不能依赖全局变量,包括其他模块。

But I would have thought that objects of the class would have to be destroyed before the class is destroyed. 但是我会想到在类被销毁之前必须销毁类的对象。 Apparently not: 显然不是:

class A(object):
    count = 0
    def __init__(self):
        A.count += 1
        print 'Creating object, there are now', A.count
    def __del__(self):
        A.count -= 1
        print 'Destroying object, there are now', A.count

a1 = A()
a2 = A()

On Windows 7 x64 Python v2.7.3 I get: 在Windows 7 x64 Python v2.7.3上,我得到:

Creating object, there are now 1
Creating object, there are now 2
Exception AttributeError: "'NoneType' object has no attribute 'count'" in <bound
 method A.__del__ of <__main__.A object at 0x0234B8B0>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'count'" in <bound
 method A.__del__ of <__main__.A object at 0x0234BE90>> ignored

I understand the implications of How do I correctly clean up a Python object? 我理解如何正确清理Python对象的含义 . But this case is for class (shared or 'static' for my C++ friends) variables. 但这种情况适用于类(我的C ++朋友的共享或'静态')变量。 Surely the instance has a reference to the class variable, which should not be destroyed first as we still have a reference to it? 当然实例有一个类变量的引用,不应该首先销毁它,因为我们仍然有引用它?

Is it a problem or a mistake to have the class destroyed before objects of that class? 在类的对象之前将类销毁是一个问题还是错误? Maybe my question is 'Where are class-shared variables actually stored?" 也许我的问题是“实际存储了类共享变量的位置?”

(Yes, I understand that this can be rectified using a context manager, or even simply: (是的,我知道这可以使用上下文管理器来纠正,甚至简单地说:

del a1
del a2

to destroy the object before the class is destroyed.) 在类被销毁之前销毁对象。)

Maybe my question is 'Where are class-shared variables actually stored?" 也许我的问题是“实际存储了类共享变量的位置?”

If that is your question, the answer is "on the class". 如果这是你的问题,那么答案是“在课堂上”。

To address your larger question: As you noted, __del__ can't rely on global variables — but A is a global variable, so you can't rely on it. 解决你的大问题:正如你所指出的, __del__不能依赖全局变量 - 但A是一个全局变量,所以你不能依赖它。 Interestingly, I can't find an official statement of this in the docs, but there are various references on the internet to this fact that __del__ can't use global variables. 有趣的是,我无法在文档中找到关于此的正式声明,但是在互联网上有各种各样的参考,即__del__不能使用全局变量。 Here is one: 是一个:

When a Python program terminates the global variables in each module are set to None. 当Python程序终止时,每个模块中的全局变量都设置为None。 The order in which this happens it undefined 发生这种情况的顺序是未定义的

It will work if you access the count via self.__class__.count instead of A.count . 如果您通过self.__class__.count而不是A.count访问self.__class__.countA.count (This is also why it works when you use self.count .) (这也是使用self.count时它的工作原理。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM