Python面向对象的脚本报错

回复 收藏
本帖最后由 kingleoric 于 2016-5-5 19:45 编辑

这个是Python简明教程里面的脚本,我仅仅只是修改了红色部分,按照道理不会报错,怎么会有问题,还请各位大神看一下。
[root@shanghai-WEB-228-102_nginx ~]# vi objvar.py
#!/usr/bin/python
#filename:objvar.py
class Person:
        population=0
        def __init__(self,name):
                self.name=name
                print '(initializing %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
kingleoric=Person('kingleoric')
kingleoric.sayhi()
kingleoric.howmany()

leon=Person('leon liang')
leon.sayhi()
leon.howmany()

kingleoric.sayhi()
kingleoric.howmany()
[root@shanghai-WEB-228-102_nginx ~]# python objvar.py
(initializing kingleoric)
hi,my name is kingleoric.
I am the only person here.
(initializing leon liang)
hi,my name is leon liang.
we have 2 persons here.
hi,my name is kingleoric.
we have 2 persons here.
leon liang say bye.
there are still 1 people left
kingleoric say bye.
Exception AttributeError: "'NoneType' object has no attribute 'population'" in > ignored

Python简明教程里的脚本为:
[root@shanghai-WEB-228-102_nginx ~]# cat objvar2.py
#!/usr/bin/env python
# Filename: objvar.py

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

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

                #When this person is created, he/she adds to the population
                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

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

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

swaroop.sayHi()
swaroop.howMany()
[root@shanghai-WEB-228-102_nginx ~]# python objvar2.py
(Initializing kingleoric)
Hi, my name is kingleoric.
I am the only person here.
(Initializing Abdul kalam)
Hi, my name is Abdul kalam.
We have 2 persons here.
Hi, my name is kingleoric.
We have 2 persons here.
Abdul kalam says bye.
There are still 1 people left.
kingleoric says bye.
I am the last one.

为毛修改了名字就报错
2016-05-05 19:43 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: