遇到一个很奇怪的问题,本人刚学习Python ,在学类与对象的方法时,在调用方法时,我的对象名取名为k ,然而死活就是报AttributeError异常; 在反复检查代码无误后,然后我试着改一下名字,奇迹出现了,名字一改就好了,我以为不能使用单个字母,抱着试一试的心态,我将对象名改为a ,奇迹再次出现,成功了!!!规则不是很懂,还请大神指教
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
swaroop=Person('swaroop')
swaroop.sayHi()
swaroop.howMany()
# 此处有问题
a=Person('Abdul Kalam')
a.sayHi()
a.howMany()
swaroop.sayHi()
swaroop.howMany()