-2

我试图存储对象并且能够成功地做到这一点..但是我遇到了问题,在寻找它时..下面给出了错误

人.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):
        #return self.pay *= (1.0 + percent)
        self.pay *= (1.0 + percent)
        return self.pay

管理器.py

from Person import Person

class Manager(Person):
    def giveRaise(self, percent, bonus=0.1):
        self.pay *= (1.0 + percent + bonus)        
        return self.pay

update_db_classes.py

import shelve
from Person import Person
from Manager import Manager
bob = Person('Bob Smith', 42, 30000, 'software')
sue = Person('Sue Jones', 45, 40000, 'hardware')
tom = Manager('Tom Doe',  50, 50000)

db = shelve.open('class-shelve')
db['bob'] = bob
db['sue'] = sue
db['tom'] = tom
db.close()

检索对象的代码.. dump_db_classes.py

import shelve
db = shelve.open('class-shelve')
for key in db:
    print(key, '=>\n  ', db[key].name, db[key].pay)

bob = db['bob']
print(bob.lastName())
print(db['tom'].lastName())

错误:C:\Python27\Basics>dump_db_classes.py bob => Bob Smith 30000 sue => Sue Jones 40000 tom => Tom Doe 50000 Traceback(最近一次调用):文件“C:\Python27\Basics\dump_db_classes.py ",第 8 行,在 print(bob.lastName()) AttributeError: 'Person' 对象没有属性 'lastName'

4

1 回答 1

1

Python 区分大小写。你打电话bob.lastName(),但Person方法是.lastname(). 错误消息试图告诉您:

AttributeError: 'Person' object has no attribute 'lastName'
于 2012-06-04T04:19:42.513 回答