类程序析构函数崩溃中的Python模糊行为

时间:2016-12-11 13:35:46

标签: python

以下Python代码完美无缺。

class X:
   __n = 0

   def __init__(self):
       X.__n += 1
       print "cons", X.__n

   def __del__(self):
       X.__n -= 1
       print "dest", X.__n

a = X()
b = X()

其中每个平台都有轻微变化的同一个

class A:
   n = 0

   def __init__(self):
       A.n += 1
       print "constructor", A.n

   def __del__(self):
       A.n -= 1
       print "destructor", A.n

a1 = A() 
a2 = A() 
constructor 1
constructor 2
Exception AttributeError: "'NoneType' object has no attribute 'n'" in <bound method A.__del__ of <__main__.A instance at 0xfff4526c>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'n'" in <bound method A.__del__ of <__main__.A instance at 0xfff4528c>> ignored

我对这种可疑行为一无所知。

0 个答案:

没有答案