论坛首页 编程语言技术论坛

简明 Python 教程里面的一个例子的问题

浏览 2371 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-24  
例子的链接:
http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch11s06.html

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()

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

swaroop.sayHi()
swaroop.howMany()


运行起来完全没有问题

但如果把测试那一段改为:
test=Person('Swaroop')
test.sayHi()
test.howMany()

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

test.sayHi()
test.howMany()

运行的时候就会得到一个异常:
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'popula
tion'" in <bound method Person.__del__ of <__main__.Person instance at 0x00BEC12
0>> ignored

然后改很多次那个变量都有这个异常,好像那个变量只有用swaroop才没有问题,非常怪异。。。。
有没有人知道这是怎么回事?

我用的python版本: Python 2.5.2 windows下面的。

   发表时间:2008-09-01  
test改为其他的单词完全没有问题,应该是test的命名有冲突吧
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics