![]() |
#2
外部三电铃2011-08-25 22:26
|
程序执行时除了一些正常的输出外,最后还报告以下错误:
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb77357cc>> ignored
请大家帮看看是哪里有问题呢?谢谢!
以下是程序源代码(代码来源于《简明Python教程》http://):
![](zzz/editor/img/code.gif)
#!/usr/bin/python
# filename : objvar.py
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person' 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
duwei = Person('duwei')
duwei.sayHi()
duwei.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
duwei.sayHi()
duwei.howMany()
# filename : objvar.py
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person' 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
duwei = Person('duwei')
duwei.sayHi()
duwei.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
duwei.sayHi()
duwei.howMany()