[英]__init__() takes 3 positional arguments but 4 were given when run
你好
我有这个代码 python 代码,试图制作一个父 class 出版物和 2 个子类(书籍和杂志)。
然后它尝试创建一个 object 并根据列表中的用户选择将其添加到列表中
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
在调用 class 方法时,您不需要显式传递self
。
它应该是
super().__init__(title, price)
代替
super().__init__(self, title, price)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.