- 论坛徽章:
- 0
|
一段很简单的code,看起来都是顺理成章的,为什么会出现这种错误提示:打开一个文件person.py
class person:
def __init__(self,name,age,pay=0,job=None):
self.name=name
self.age=age
self.pay=pay
self.job=job
def lastname(self):
return self.name.split()[-1]
def giveraise(self,percent):
self.pay*=(1.0+percent)
if __name__ == '__main__':
bob=person('bob smith',42,30000,'sweng')
sue=person('sue jones',45,40000,'music')
print bob.name,sue.pay
print bob.lastname()
sue.giveraise(.10)
print sue.pay
这个文件运行没问题,结果为:
bob smith 40000
smith
44000.0
然后再写一个subclass,import person,如下,保存为文件manager.py:
from person import person
class manager(person):
def giveraise(self,percent,bonus=0.1):
self.pay*=(1.0+percent+bonus)
if __name__=='__main__':
tom =manager(name='tom doe',age=50,pay=50000)
print tom.lastname()
tom.giveraise(.20)
print tom.pay()
运行如下:
doe
Traceback (most recent call last):
File "manager.py", line 9, in <module>
print tom.pay()
TypeError: 'float' object is not callable
只有第一个print 运行出来了,第二个出错了,为什么呢?我检查了好几遍,都看不出问题,谁能帮我看一下,到底为什么? |
|