Python 类与对象变量

发现一个无比诡异的现象。当创建对象为JACK的时候。就会出现很奇怪的错误。。

到现在未明白,百度了下说是变量到某个地方没有传递成功。。蛋疼。。

难道Python封杀了JACK?。。

Exception AttributeError: "'NoneType' object has no attribute 
'population'" in <bound method Person.__del__ of
 <__main__.Person instance at 0x7f42b2b596c8>> ignored

就是这上面的错误,比较无奈,用其他名称果断成功。。。!!!

#!/usr/bin/python

class Person:
    population=0

    def __init__(self,name):
        self.name=name
        print '(Initalizing %s)' % self.name
        Person.population+=1

    def __del__(self):
        print '%s say 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):
        print 'Hi,my name is %s' % self.name

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


ken=Person('Ken lin')
ken.sayHi()
ken.howMany()

jackin=Person('Jacki')
jackin.sayHi()
jackin.howMany()

swaroop=Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

kalam=Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
                                          

 

posted @ 2013-03-06 12:17  墨迹哥's  阅读(207)  评论(0编辑  收藏  举报