Python python __def__ Exception AttributeError: "'NoneType' object has no attribute

class Person:
    '''Represents a person.'''
    population = 0

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

        Person.population +=1

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

        Person.population -=1

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

    def sayHi(self):
        '''Greeting by the person.

        Really, that's all it does.'''
        print 'Hi, my name is %s.' % self.name

    def howMany(self):
        '''Prints the current population.'''
        if Person.population == 1:
            print 'I am the only person here.'
        else:
            print 'We have %d persons here.' % Person.population

jerry = Person('Jerry')
jerry.sayHi()
jerry.howMany()

qiu = Person('Qiu')
qiu.sayHi()
qiu.howMany()

jerry.sayHi()
jerry.howMany()

出现如下错误:

Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0x01AF97D8>> ignored
原因如下:
At interpreter shutdown, the module's global variables are set to None before the module itself is released.
__del__ methods may be called in those precaries circumstances, and should not rely on any global state.
将__del__方法中对类变量的访问方式改为如下即可:
def __del__(self): 
   self.__class__.population -= 1

 

最近学习《Python参考手册》学到Class部分,遇到了类的构造析构部分的问题:

1、什么时候构造?

2、什么时候析构?

3、成员变量如何处理?

4、Python中的共享成员函数如何访问?

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

探索过程:

1、经过查找,Python中没有专用的构造和析构函数,但是一般可以在__init__和__del__分别完成初始化和删除操作,可用这个替代构造和析构。还有一个__new__用来定制类的创建过程,不过需要一定的配置,此处不做讨论。 

2、类的成员函数默认都相当于是public的,但是默认开头为__的为私有变量,虽然是私有,但是我们还可以通过一定的手段访问到,即Python不存在真正的私有变量。如:

__priValue = 0 # 会自动变形为"_类名__priValue"的成员变量  

3、由于Python的特殊性,全局成员变量是共享的,所以类的实例不会为它专门分配内容空间,类似于static,具体使用参看下面的例子。

测试1:

# encoding:utf8  
  
class NewClass(object):  
    num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配  
    def __init__(self,name):  
        self.name = name  
        NewClass.num_count += 1  
        print name,NewClass.num_count  
    def __del__(self):  
        NewClass.num_count -= 1  
        print "Del",self.name,NewClass.num_count  
    def test():  
        print "aa"  
  
aa = NewClass("Hello")  
bb = NewClass("World")  
cc = NewClass("aaaa")  
  
print "Over"  

 

调试运行:

Hello 1  
World 2  
aaaa 3  
Over  
DeException l Hello 2  
AttributeError: "'NoneType' object has no attribute 'num_count'" in <bound method NewClass.__del__ of <__main__.NewClass object at 0x01AF18D0>> ignored  
Exception AttributeError: "'NoneType' object has no attribute 'num_count'" in <bound method NewClass.__del__ of <__main__.NewClass object at 0x01AF1970>> ignored  

 

我们发现,num_count 是全局的,当每创建一个实例,__init__()被调用,num_count 的值增一,当程序结束后,所有的实例会被析构,即调用__del__() 但是此时引发了异常。查看异常为 “NoneType” 即 析构时NewClass 已经被垃圾回收,所以会产生这样的异常。

        但是,疑问来了?为什么会这样?按照C/C++等语言的经验,不应该这样啊!经过查找资料,发现:

        Python的垃圾回收过程与常用语言的不一样,Python按照字典顺序进行垃圾回收,而不是按照创建顺序进行。所以当系统进行回收资源时,会按照类名A-Za-z的顺序,依次进行,我们无法掌控这里的流程。

     明白这些,我们做如下尝试:

# encoding:utf8  
  
class NewClass(object):  
    num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配  
    def __init__(self,name):  
        self.name = name  
        NewClass.num_count += 1  
        print name,NewClass.num_count  
    def __del__(self):  
        NewClass.num_count -= 1  
        print "Del",self.name,NewClass.num_count  
    def test():  
        print "aa"  
  
aa = NewClass("Hello")  
bb = NewClass("World")  
cc = NewClass("aaaa")  
  
del aa  
del bb  
del cc  
  
print "Over"  

 调试输出:

Hello 1  
World 2  
aaaa 3  
Del Hello 2  
Del World 1  
Del aaaa 0  
Over  

 

OK,一切按照我们预料的顺序发生。

但是,我们总不能每次都手动回收吧?这么做Python自己的垃圾回收还有什么意义?

SO,继续查找,我们还可以通过self.__class__访问到类本身,然后再访问自身的共享成员变量,即 self.__class__.num_count , 将类中的NewClass.num_count替换为self.__class__.num_count 编译运行,如下:

# encoding:utf8  
  
class NewClass(object):  
    num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配  
    def __init__(self,name):  
        self.name = name  
        self.__class__.num_count += 1  
        print name,NewClass.num_count  
    def __del__(self):  
        self.__class__.num_count -= 1  
        print "Del",self.name,self.__class__.num_count  
    def test():  
        print "aa"  
  
aa = NewClass("Hello")  
bb = NewClass("World")  
cc = NewClass("aaaa")  
  
print "Over"  

  结果:

Hello 1  
World 2  
aaaa 3  
Over  
Del Hello 2  
Del World 1  
Del aaaa 0  

  

Perfect!我们完美地处理了这个问题!

PS:

书上又提到了一些问题,在这里作补充(仅作为参考):

__new__()是唯一在实例创建之前执行的方法,一般用在定义元类时使用。

del xxx 不会主动调用__del__方法,只有引用计数==0时,__del__()才会被执行,并且定义了__del_()的实例无法被Python的循环垃圾收集器收集,所以尽量不要自定义__del__()。一般情况下,__del__() 不会破坏垃圾处理器。

实验中发现垃圾回收自动调用了__del__, 这与书上所说又不符,不知是什么原因,需要继续学习。

原文地址:https://www.cnblogs.com/monkeyfather/p/4166993.html