Exception AttributeError: "'NoneType' object has no attribute 'population'"
344276105
shuimulinxi at foxmail.com
Mon Aug 24 01:49:49 EDT 2015
Hi all,
I am a python learner. I encountered a problem when i was testing the following code. What is strange is that if I replace the object name with zhang, the program would be ok. And if I replace the Person.population with self.__class__.population, it will also be ok. So what is the matter?
Here is the code,
#!/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 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):
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
zhangsan = Person('Swaroop')
zhangsan.sayHi()
zhangsan.howMany()
lisi = Person('LiSi')
lisi.sayHi()
lisi.howMany()
zhangsan.sayHi()
zhangsan.howMany()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150824/34e33f5d/attachment.html>
More information about the Python-list
mailing list