Python __init_;()接受3个位置参数,但在运行时提供了4个
嗨 我有一段python代码,试图发布一个父类和两个子类(book和magazine) 然后它尝试创建一个对象,并根据用户从列表中的选择将其添加到列表中Python __init_;()接受3个位置参数,但在运行时提供了4个,python,python-3.x,oop,inheritance,Python,Python 3.x,Oop,Inheritance,嗨 我有一段python代码,试图发布一个父类和两个子类(book和magazine) 然后它尝试创建一个对象,并根据用户从列表中的选择将其添加到列表中 class Publication: title="" price=0.0 def __init__(self,title,price): self.title=title self.price=price def display(self):
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
出版系统
选择一项服务:
1) 添加书本
2) 添加杂志
3) 显示图书目录
4) 显示杂志列表
5) 出口
输入选项:1
输入书名:python基础知识
进入书的作者:埃里克
输入图书价格:40
回溯(最近一次呼叫最后一次):
文件“/Users/zahraal nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py”,第64行,在
书=书(书名、书价、书作者)
文件“/Users/zahraal nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py”,第19行,在__
super().\uuuu init\uuuuuu(自我、头衔、价格)
TypeError:\uuuu init\uuuuuu()接受3个位置参数,但给出了4个
我该怎么解决呢?我见过类似的问题,但没有找到答案。super()。\uuu init\uu
是一种绑定方法,已经包含了self
。您不需要显式地传递self
。比如说,
class Book(Publication):
def __init__(self, title, price, author):
super().__init__(title, price)
self.author = author
# etc
super()。您不需要显式地传递self
。比如说,
class Book(Publication):
def __init__(self, title, price, author):
super().__init__(title, price)
self.author = author
# etc
调用类方法时,不需要显式传递self
应该是
super().__init__(title, price)
而不是
super().__init__(self, title, price)
调用类方法时,不需要显式传递self
应该是
super().__init__(title, price)
而不是
super().__init__(self, title, price)