Hello, I am new to programming with python. I am using the tutorial, "Byte of Python" and am on p. 82. I have come across something very unusual by accident and I was wondering if anybody here could figure it out. In the program attached, if I use "wanless.sayHI( )" which is my name,python gives an error message........ .Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Perso n instance at 0xb76056ec>> ignored..... Any of the commented out groups of code do the same. I was looking for a bad indent or missing ' or anything for about an hour. Once I figured it out I thought is was so funny I had to send it to someone who knows python just to see what you think of it. I used both "Idle using Python2.6.2", and gedit 2.26.1, to create the program and I get the same results with either one. I use Ubuntu 9.04 the original program is from the book "Byte of Python" p.82 2003-2005 edition
result of program as is
result of code with block 1 uncommented
the code is exactly the same as the book down to line 35
result of program as is
Code:
(Initializing Swaroop) Hi, my name is Swaroop. I am the only person here. (Initializing less) Hi, my name is less. We have 2 persons here. (Initializing wanles) Hi, my name is wanles. We have 3 persons here. Hi, my name is Swaroop. We have 3 persons here. wanles says bye. There are still 2 people left. less says bye. There are still 1 people left. Swaroop says bye. I am the last one.
Code:
(Initializing Swaroop) Hi, my name is Swaroop. I am the only person here. (Initializing wanless) Hi, my name is wanless. We have 2 persons here. (Initializing less) Hi, my name is less. We have 3 persons here. (Initializing wanles) Hi, my name is wanles. We have 4 persons here. Hi, my name is Swaroop. We have 4 persons here. wanles says bye. There are still 3 people left. less says bye. There are still 2 people left. Swaroop says bye. There are still 1 people left. wanless says bye. Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb766f48c>> ignored
Code:
#!/usr/bin/python
# Filename: objvar.py
# Mark Wanless
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('Swaroop')
swaroop.sayHi()
swaroop.howMany()
less = Person('less') ### no problem
less.sayHi()
less.howMany()
wanles = Person('wanles')### no problem
wanles.sayHi()
wanles.howMany()
############when I uncommment any of these below
############I get the error message
######## block 1 ##############
#wanless = Person('wanless')
#wanless.sayHi()
#wanless.howMany()
######## block 1 #############
#les = Person('les')
#les.sayHi()
#les.howMany()
#wanle = Person('wanle')
#wanle.sayHi()
#wanle.howMany()
swaroop.sayHi()
swaroop.howMany()
Comment