Python - 类型错误:module.__init__() 最多接受 2 个参数(给出 3 个)

用户2693371

尝试 Python 继承。我需要您帮助解决错误。

我有 2 个班级:Person(超级班级)和 Contact(子班级)。

尝试运行 Contact 时出现以下错误:

        "Contact.py", line 3, in <module>
            class Contact(Person):
        TypeError: module.__init__() takes at most 2 arguments (3 given)

提前致谢

下面是我的代码:

class Person:
      __name=""
      __age=0

      def __init__(self, name, age):
          self.__name = name
          self.__age = age

      def set_name(self, name):
          self.__name = name

      def set_age(selfself, age):
          self.__age = age

      def get_name(self):
          return self.__name

      def get_age(selfself):
          return self.__age

      def  getInfo(self):
        return "Name is: {} - Age is: {}".format(self.__name, self.__age)

        # ----------------------------------------------------

    import Person
            class Contact(Person):
        __method=""

        def __init__(self, name, age, method):
            super().__init__(name, age)
            self.__method = method



        def set_method(self, method):
            self.__method = method

        def get__method(self):
            return self.__method

        def getInfo(self):
            return "Name is: {} - Age is: {} - Contact Info: {}".format(self.__name, self.__age, self.__method)


    person2 = Contact("Adam Smith", 19, "Email: [email protected]")
    print(person2.getInfo())
罗莉卡

首先,缩进搞砸了!

如果 Person 在单独的文件中,请导入不带扩展名的文件名,如下所示:

class Person:

  def __init__(self, name, age):
      self.__name = name
      self.__age = age

  def set_name(self, name):
      self.__name = name

  def set_age(self, age):
      self.__age = age

  def get_name(self):
      return self.__name

  def get_age(self):
      return self.__age

  def getInfo(self):
      return "Name is: {} - Age is: {}".format(self.__name, self.__age)

    # ----------------------------------------------------

from person import Person # assumed your Person class is in person.py

class Contact(Person):
    __method=""

    def __init__(self, name, age, method):
        super().__init__(name, age)
        self.__method = method

    def set_method(self, method):
        self.__method = method

    def get__method(self):
        return self.__method

    def getInfo(self):
        return "Name is: {} - Age is: {} - Contact Info: {}".format(self.get_name(), self.get_age(), self.__method)


person2 = Contact("Adam Smith", 19, "Email: [email protected]")
print(person2.getInfo())

通过其方法访问父类的私有字段。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

module .__ init __()在Python中最多接受2个参数错误

来自分类Dev

导入模块导致TypeError:module .__ init __()最多接受2个参数(给定3个)

来自分类Dev

Python错误“ TypeError:sort()最多接受2个参数(给定3个)”

来自分类Dev

类型错误:center() 最多接受 2 个参数(给出 4 个)

来自分类Dev

sendMessage()在wxPython中最多接受3个参数(给定4个)错误

来自分类Dev

TypeError: __init__() 需要 2 个位置参数,但 Python 3 给出了 3 个?

来自分类Dev

python super :TypeError: __init__() 需要 2 个位置参数,但给出了 3 个

来自分类Dev

Python:__init __()接受2个位置参数,但给出了3个

来自分类Dev

从内置类型派生的Python类将不会构造:TypeError:type()最多接受X个参数(给定Y)

来自分类Dev

错误:类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

来自分类Dev

陷入类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

来自分类Dev

TypeError:execute()最多接受3个参数(给定11个)

来自分类Dev

Python - 子类 TypeError:最多需要 2 个参数(给出 3 个),而应该需要 3 个

来自分类Dev

rsync命令在命令行中最多接受2个参数

来自分类Dev

Money和TypeError:__init __()接受1到2个位置参数,但给出了3个

来自分类Dev

多重继承,TypeError:__init __()接受2个位置参数,但给出了3个

来自分类Dev

Money和TypeError:__init __()接受1到2个位置参数,但给出了3个

来自分类Dev

__init__() 需要 2 个位置参数,但 3 个是使用 WebDriverWait 和 expected_conditions 作为 element_to_be_clickable 和 Selenium Python 给出的

来自分类Dev

Python-peewee接受2个位置参数,但给出了3个

来自分类Dev

意外错误:replace()接受2个位置参数,但给出了3个

来自分类Dev

这里抛出了TypeError异常:init()接受2个位置参数,但使用Selenium和Python的显式等待给出3个

来自分类Dev

TypeError:__init __()接受1个位置参数,但给出了2个(使用Pytesseract的Python多处理)

来自分类Dev

类型错误:__init__() 需要 4 个位置参数,但给出了 7 个

来自分类Dev

Python 2.7 __init __()恰好接受2个参数(给定3个)

来自分类Dev

Python 2.7 __init __()恰好接受2个参数(给定3个)

来自分类Dev

类型错误:withColumn() 正好需要 3 个参数(给出 2 个)

来自分类Dev

Django错误:__init __()恰好接受2个参数(给定3个)

来自分类Dev

__init __()接受2个位置参数,但是使用present_of_element_located()给出了3个试图等待元素的参数

来自分类Dev

TypeError:__init __()接受1个位置参数,但给出了3个

Related 相关文章

  1. 1

    module .__ init __()在Python中最多接受2个参数错误

  2. 2

    导入模块导致TypeError:module .__ init __()最多接受2个参数(给定3个)

  3. 3

    Python错误“ TypeError:sort()最多接受2个参数(给定3个)”

  4. 4

    类型错误:center() 最多接受 2 个参数(给出 4 个)

  5. 5

    sendMessage()在wxPython中最多接受3个参数(给定4个)错误

  6. 6

    TypeError: __init__() 需要 2 个位置参数,但 Python 3 给出了 3 个?

  7. 7

    python super :TypeError: __init__() 需要 2 个位置参数,但给出了 3 个

  8. 8

    Python:__init __()接受2个位置参数,但给出了3个

  9. 9

    从内置类型派生的Python类将不会构造:TypeError:type()最多接受X个参数(给定Y)

  10. 10

    错误:类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

  11. 11

    陷入类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

  12. 12

    TypeError:execute()最多接受3个参数(给定11个)

  13. 13

    Python - 子类 TypeError:最多需要 2 个参数(给出 3 个),而应该需要 3 个

  14. 14

    rsync命令在命令行中最多接受2个参数

  15. 15

    Money和TypeError:__init __()接受1到2个位置参数,但给出了3个

  16. 16

    多重继承,TypeError:__init __()接受2个位置参数,但给出了3个

  17. 17

    Money和TypeError:__init __()接受1到2个位置参数,但给出了3个

  18. 18

    __init__() 需要 2 个位置参数,但 3 个是使用 WebDriverWait 和 expected_conditions 作为 element_to_be_clickable 和 Selenium Python 给出的

  19. 19

    Python-peewee接受2个位置参数,但给出了3个

  20. 20

    意外错误:replace()接受2个位置参数,但给出了3个

  21. 21

    这里抛出了TypeError异常:init()接受2个位置参数,但使用Selenium和Python的显式等待给出3个

  22. 22

    TypeError:__init __()接受1个位置参数,但给出了2个(使用Pytesseract的Python多处理)

  23. 23

    类型错误:__init__() 需要 4 个位置参数,但给出了 7 个

  24. 24

    Python 2.7 __init __()恰好接受2个参数(给定3个)

  25. 25

    Python 2.7 __init __()恰好接受2个参数(给定3个)

  26. 26

    类型错误:withColumn() 正好需要 3 个参数(给出 2 个)

  27. 27

    Django错误:__init __()恰好接受2个参数(给定3个)

  28. 28

    __init __()接受2个位置参数,但是使用present_of_element_located()给出了3个试图等待元素的参数

  29. 29

    TypeError:__init __()接受1个位置参数,但给出了3个

热门标签

归档