__del__上的Python attributeError [英] Python attributeError on __del__
问题描述
我有一个python类对象,我想分配一个类变量的值
I have a python class object and I want to assign the value of one class variable
class Groupclass(Workerclass):
"""worker class"""
count = 0
def __init__(self):
"""initialize time"""
Groupclass.count += 1
self.membercount = 0;
self.members = []
def __del__(self):
"""delte a worker data"""
Groupclass.count -= 1
if __name__ == "__main__":
group1 = Groupclass()
b $ b
这个执行结果是正确的,但是有一个错误信息:
This execution result is correct, but there's an error message that says:
Exception AttributeError: "'NoneType' object has no attribute 'count'" in <bound method Groupclass.__del__ of <__main__.Groupclass instance at 0x00BA6710>> ignored
有人能告诉我我做错了什么?
Can someone tell me what me I did wrong?
推荐答案
您的 __ del __
方法假设类仍然存在
Your __del__
method assumes that the class is still present by the time it is called.
这个假设是不正确的。 当您的Python程序退出时,Groupclass
已被清除,现在设置为无
。
This assumption is incorrect. Groupclass
has already been cleared when your Python program exits and is now set to None
.
测试类的全局引用是否仍然存在:
Test if the global reference to the class still exists first:
def __del__(self):
if Groupclass:
Groupclass.count -= 1
或使用 type()
以获得本地引用:
or use type()
to get the local reference:
def __del__(self):
type(self).count -= 1
但请注意,这意味着如果 Groupclass
是子类的(每个子类获得 .count
属性与 Groupclass
具有 .count
属性)。
but do note that this means that the semantics for count
change if Groupclass
is subclassed (each subclass gets a .count
attribute versus only Groupclass
having a .count
attribute).
从 __ del __
钩子文档引用:
警告:由于调用
__ del __()
方法的不稳定情况,将忽略执行期间发生的异常,并且会向sys.stderr
。另外,当响应于被删除的模块(例如,当程序的执行完成时)调用__ del __()
时,由__ del __()
方法可能已经被删除或正在被删除的过程中(例如导入机器关闭)。因此,__ del __()
方法应该保持外部不变量所需的绝对最小值。从版本1.5开始,Python保证其名称以单个下划线开头的全局变量在删除其他全局变量之前从其模块中删除;如果没有其他对这样的全局变量的引用,这可能有助于确保调用__ del __()
方法时导入的模块仍然可用。
Warning: Due to the precarious circumstances under which
__del__()
methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed tosys.stderr
instead. Also, when__del__()
is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the__del__()
method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason,__del__()
methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the__del__()
method is called.
如果您使用Python 3,还需要另外两个注释:
If you are using Python 3, two additional notes apply:
-
CPython 3.3自动应用随机散列盐值到
全局字符
字典中使用的str
这也影响了清除全局变量的顺序,并且可以 是
CPython 3.3 automatically applies a randomized hash salt to the
str
keys used in aglobals
dictionary; this also affects the order in which globals are cleaned up, and it could be that you see the problem on only some of the runs.
CPython 3.4不再将全局变量设置为无
(在大多数情况下),根据安全对象完成;请参阅 PEP 442 。
CPython 3.4 no longer sets globals to None
(in most cases), as per Safe Object Finalization; see PEP 442.
这篇关于__del__上的Python attributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!