【问题标题】:TypeError: __init__() takes 2 positional arguments but 4 were givenTypeError: __init__() 接受 2 个位置参数,但给出了 4 个
【发布时间】:2019-12-07 01:20:41
【问题描述】:

我的代码给出的错误为TypeError: __init__() takes 2 positional arguments but 4 were given。试图寻找一个额外的论点,但找不到。

尝试了以前回答的问题,但没有得到任何适当的解决方案。

我的代码如下:

from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):
    def __init__(self,title,author):
        self.title=title
        self.author=author   
    @abstractmethod
    def display(): pass

#Write MyBook class
class MyBook(Book):
    def __init__(self, price):
        self.price = price

    def display():
        print('Title: {}'.format(title))
        print('Author: {}'.format(author))
        print('Price: {}'.format(price))

title=input()
author=input()
price=int(input())
new_novel=MyBook(title,author,price)
new_novel.display()

编译器给出如下运行时错误

Traceback (most recent call last):
  File "Solution.py", line 23, in <module>
    new_novel=MyBook(title,author,price)
TypeError: __init__() takes 2 positional arguments but 4 were given

【问题讨论】:

  • 要添加到@gbtimmon,特别是解释错误消息-MyBook(title,author,price) 将传递四个参数:(self, title, author, price)。同时,MyBook.__init__ 被定义为只期望两个:(self, price)。 (您在 Book 中写的内容无关紧要。)因此错误消息(“__init__() 接受 2 个位置参数,但给出了 4 个”)。

标签: python arguments positional-parameter


【解决方案1】:

代码内的注释。

from abc import ABCMeta, abstractmethod


class Book(object, metaclass=ABCMeta): # 1. Why do you need meta class?
    def __init__(self, title, author):
        self.title=title
        self.author=author
    @abstractmethod
    def display(self): pass  # 2. Consider replacing with  raise NotImplementedError + missing 'self'


class MyBook(Book):
    def __init__(self, price, title, author):  # 3 You are missing two arguments in here.... (title and author)
        super(MyBook, self).__init__(title, author)  # 4 This line is missing
        self.price = price

    def display(self):
        print('Title: {}'.format(self.title))  # self missing in all three lines
        print('Author: {}'.format(self.author))
        print('Price: {}'.format(self.price))

title=input()
author=input()
price=int(input())
new_novel = MyBook(title, author, price)
new_novel.display()

【讨论】:

  • 其实我是从一个竞争激烈的编程网站 HackerRank 解决这个问题的。 Book() 类已定义且不可编辑。我可以为解决方案编写 MyBook() 类。甚至不能改变传递的参数
  • 好的,所以实现 MyBook 类中的注释 - 它应该可以解决您的问题。
  • 非常感谢您的帮助
【解决方案2】:

Python 不会自动调用父类的 init。你需要明确地做到这一点。

from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):
    def __init__(self,title,author):
        self.title=title
        self.author=author   
    @abstractmethod
    def display(): pass

#Write MyBook class
class MyBook(Book):
    def __init__(self, price, title, author):
        super(MyBook, self).__init__(title, author)
        self.price = price

    def display():
        print('Title: {}'.format(title))
        print('Author: {}'.format(author))
        print('Price: {}'.format(price))

【讨论】:

  • 这似乎不是唯一的问题。例如。 display 没有引用类变量。
  • 将 def display() 更改为 def display(self) 解决了问题
  • 感谢您的及时回复
  • @Carsten 然而,产生错误 OP 的问题是发布的。我不想为他做所有他的家庭作业:)
猜你喜欢
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 2017-04-22
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多