python - __init__() 接受 3 个位置参数,但在运行时给出了 4 个
问题描述
你好
我有这段代码python代码,试图制作一个父类出版物和2个子类(书籍和杂志)。
然后它尝试创建一个对象并根据用户从列表中的选择将其添加到列表中
class Publication:
title=""
price=0.0
def __init__(self,title,price):
self.title=title
self.price=price
def display(self):
print("title: "+self.title)
print("\nprice: "+str(self.price))
class Book(Publication):
author=""
def __init__(self,title,price,author):
super().__init__(self,title,price)
self.author=author
def display(self):
print("title: "+self.title)
print(", price: "+ str(self.price))
print(", author: "+self.author)
class Magazine(Publication):
issue = ""
def __init__(self, title, price, issue):
super().__init__(self, title, price)
self.issue = issue
def display(self):
print("title: " + self.title)
print(", price: " + str(self.price))
print(", issue: " + self.issue)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
choice=1;
bookList=[]
MagazineList=[]
while(choice!=0):
print("Publication System")
print("Select a service:")
print("1) Add Book")
print("2) Add Magazine")
print("3) Display Book Lists")
print("4) Display Magazine Lists")
print("5) Exit")
choice = int(input("Enter choice: "))
if choice == 1:
bookTitle=str(input("Enter the book title: "))
bookAuthor = str(input("Enter the book author: "))
bookPrice = float(input("Enter the book price: "))
book=Book(bookTitle,bookPrice,bookAuthor)
bookList.append(book)
elif choice == 2:
MagazineTitle=str(input("Enter the magazine title: "))
MagazineIssue = str(input("Enter the magazine issue: "))
MagazinePrice = float(input("Enter the magazine price: "))
magazine=Magazine(MagazineTitle,MagazinePrice,MagazineIssue)
MagazineList.append(magazine)
elif choice == 3:
for i in range(len(bookList)):
print(bookList[i]+"\n")
elif choice == 4:
for i in range(len(MagazineList)):
print(MagazineList[i]+"\n")
elif choice == 5:
print("Exiting!")
else:
print("Invalid choice!!")
当我运行它时,它向我显示了这个错误:
Publication System
Select a service:
1) Add Book
2) Add Magazine
3) Display Book Lists
4) Display Magazine Lists
5) Exit
Enter choice: 1
Enter the book title: python fundamentals
Enter the book author: eric
Enter the book price: 40
Traceback (most recent call last):
File "/Users/zahraaal-nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py", line 64, in <module>
book=Book(bookTitle,bookPrice,bookAuthor)
File "/Users/zahraaal-nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py", line 19, in __init__
super().__init__(self,title,price)
TypeError: __init__() takes 3 positional arguments but 4 were given
我该如何解决?我见过类似的问题,但没有找到答案
解决方案
super().__init__
是一个绑定方法,已经包括self
. 您不需要self
显式传递。例如,
class Book(Publication):
def __init__(self, title, price, author):
super().__init__(title, price)
self.author = author
# etc
推荐阅读
- sql - SAP HANA 随机数生成器
- python - WxPython 选择字段仅在 Mac 上不可见
- python - 如何检索 Spotify 上的所有专辑对象?
- python - Nuitka TypeError 类型“生成器”的编译器错误对象没有 len()
- canvas - 在paperjs中以形状显示边界矩形
- angular - Angular 6:从后端服务器获取文件对象后下载文件
- sql - 使用 UNPIVOT 将代码从 sql 重写为 redshift
- c# - 遍历单个实体到 HTML 无序列表
- google-cloud-endpoints-v2 - 端点框架客户端库版本
- regex - matching string using regular expression