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__, 這與書上所說又不符,不知是什么原因,需要繼續學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM