Python中调用对象属性失败,请求排查代码问题
代码问题修正与解释
核心问题与修复:
- 拼写错误:
editbook方法中self.publsiher是拼写错误,改为self.publisher即可修复属性赋值失败的问题。
除此之外,代码还有几处Python OOP使用错误,一并修正如下:
__init__参数不匹配
原构造函数只接收3个参数,但创建实例时传入了9个属性值,需要补充完整的属性初始化:
def __init__(self, title, publisher, edition, year, month, language, paperback, isbn10, isbn13): self.title = title self.publisher = publisher self.edition = edition self.year = year self.month = month self.language = language self.paperback = paperback self.isbn10 = isbn10 self.isbn13 = isbn13
infofile改为类方法
原方法没有类/实例参数,无法通过实例调用,添加@classmethod装饰器并使用cls创建实例:
@classmethod def infofile(cls, filename): books = [] try: with open(filename, "r") as file: for line in file: bookinfo = line.strip().split(",") title,publisher,edition,year,month,language,paperback,isbn10,isbn13 = bookinfo book = cls(title,publisher,int(edition),int(year),int(month),language,paperback,int(isbn10),int(isbn13)) books.append(book) except FileNotFoundError: print(f"File not found:{filename}") return books
displaybook遍历逻辑错误
遍历书籍列表时,应该用循环变量book访问当前实例的属性,而非self(self是调用该方法的实例):
def displaybook(self): books = Book.infofile(input("Enter File name:")) for book in books: print(f"Title:{book.title}") time.sleep(1) print(f"Publisher:{book.publisher}") time.sleep(1) print(f"Edition:{book.edition}") time.sleep(1) print(f"Year:{book.year}") time.sleep(1) print(f"Month:{book.month}") time.sleep(1) print(f"Language:{book.language}") time.sleep(1) print(f"Paperback:{book.paperback}") time.sleep(1) print(f"ISBN-10:{book.isbn10}") time.sleep(1) print(f"ISBN-13:{book.isbn13}")
完整修正后的代码:
import time import string class Book: """ This is a class with methods regarding the book itself """ bookcopies = 0 #variable to keep track of bookcopies, initially = 0, once a book is added, update to 1 @classmethod def infofile(cls, filename): books = [] try: with open(filename, "r") as file: for line in file: bookinfo = line.strip().split(",") title,publisher,edition,year,month,language,paperback,isbn10,isbn13 = bookinfo book = cls(title,publisher,int(edition),int(year),int(month),language,paperback,int(isbn10),int(isbn13)) books.append(book) except FileNotFoundError: print(f"File not found:{filename}") return books def displaybook(self): books = Book.infofile(input("Enter File name:")) for book in books: print(f"Title:{book.title}") time.sleep(1) print(f"Publisher:{book.publisher}") time.sleep(1) print(f"Edition:{book.edition}") time.sleep(1) print(f"Year:{book.year}") time.sleep(1) print(f"Month:{book.month}") time.sleep(1) print(f"Language:{book.language}") time.sleep(1) print(f"Paperback:{book.paperback}") time.sleep(1) print(f"ISBN-10:{book.isbn10}") time.sleep(1) print(f"ISBN-13:{book.isbn13}") def __init__(self, title, publisher, edition, year, month, language, paperback, isbn10, isbn13): self.title = title self.publisher = publisher self.edition = edition self.year = year self.month = month self.language = language self.paperback = paperback self.isbn10 = isbn10 self.isbn13 = isbn13 def editbook(self, newtitle, newpublisher, newedition): self.title = newtitle self.publisher = newpublisher self.edition = newedition
内容的提问来源于stack exchange,提问作者Sara Mansour
相关产品推荐
相关产品推荐

