【问题标题】:importing module causes TypeError: module.__init__() takes at most 2 arguments (3 given)导入模块导致 TypeError: module.__init__() 最多接受 2 个参数(给定 3 个)
【发布时间】:2016-05-23 21:10:42
【问题描述】:

请不要标记为重复,其他类似的问题没有解决我的问题。

这是我的设置

/main.py
/actions/ListitAction.py
/actions/ViewAction.py

Main.py:

from actions import ListitAction, ViewAction

ListitAction.py:

class ListitAction(object):

    def __init__(self):
        #some init behavior

    def build_uri():
        return "test.uri"

ViewAction.py

from actions import ListitAction

class ViewAction(ListitAction):

    def __init__(self, view_id):
        ListitAction.__init__(self)
        self.view_id = view_id

    def build_uri():
        return "test"

跑步:

$ python3 main.py

我收到的唯一错误消息是:

Traceback (most recent call last):
  File "/home/jlevac/workspace/project/listit.py", line 11, in <module>
    from actions import ListitAction, ViewAction, CommentsAction
  File "/home/jlevac/workspace/project/actions/ViewAction.py", line 3, in <module>
    class ViewAction(ListitAction):
TypeError: module.__init__() takes at most 2 arguments (3 given)

即使我尝试使用 python3 控制台,我也会收到相同的错误消息:

$python3
from actions import ViewAction

我是 Python 新手,但对编程并不陌生。我假设我的错误消息与导入语句有关,但根据该消息我无法真正弄清楚它的含义。

【问题讨论】:

  • 您是否定义了 __init__.py 文件,或者您是否在 python 路径中包含了您的目录?如果您将所有内容都放在主文件中,该示例是否有效?
  • @ShadowRanger 虽然“可能重复”问题的标题与这个问题几乎相同,但两个问题中引发错误的原因是不同的(即那个问题的答案对我没有帮助,而这里接受的答案是)

标签: python python-3.x


【解决方案1】:

您的导入错误,因此您尝试从模块本身继承,而不是从其中定义的类(同名)继承。

from actions import ListitAction

ViewAction.py 中应该是:

from actions.ListitAction import ListitAction

同样,所有其他用途应切换到显式导入 from actions.XXX import XXX(感谢重复的名称),例如from actions import ListitAction, ViewAction 必须成为两个导入:

from actions.ListitAction import ListitAction
from actions.ViewAction import ViewAction

因为被导入的类来自actions 包下的不同模块。

【讨论】:

  • Python 不能使用相对路径,所以这取决于 __init__.py 的设置方式。如果他将所有内容导入到全局 action 空间中,它应该像 OP 那样工作。如果不是,你的答案是正确的。
  • 这是遵循CamelCase中只有类名,模块名应该小写的命名约定的原因之一;更容易看出您没有在您认为的位置导入类名。
  • @MSeifert:OP 得到的错误清楚地表明,他们没有在__init__.py 中做任何聪明的事情来获得这种行为;该错误消息正是您尝试从模块继承类时所期望看到的。
  • 我来自 Java 背景。所以一个文件不是一个特定的类,而是一个模块。明白了。我没有 init.py 设置,我对此一无所知。
  • @levacjeep:我很惊讶它居然还能工作;您至少需要一个空的 __init__.py 才能使软件包完全正常工作。无论如何,是的,Java 很奇怪,因为它只允许为每个源文件定义一个(非嵌套)类。 Python 和大多数其他 OO 语言可以为每个模块定义许多类。鉴于您的用例(假设它不够复杂,无法证明拆分为单独的源文件),您可能应该只创建一个 actions.py 文件并在其中定义两个类,这样可以省去这些单独导入的麻烦。
【解决方案2】:

当您不需要时,您正在传递self,仅此而已。
编辑:请参阅 MSeifert 的 comment 答案,因为我不想窃取内容。

【讨论】:

  • 在哪里?在子类中?
【解决方案3】:

如果你的文件在项目的根目录下,那么你可以直接写文件名并导入。

例如,如果文件名是Parent1.py,类名是Parent,那么你应该写

from Parent1 import Parent

但是,如果您的文件 Parent1.py 位于任何文件夹下,例如:

DemoFolder ->  Parent1.py- >    Parent
(Folder).       (File).      (Class name)

那么你必须写:

from Test.Parent1 import Parent

【讨论】:

    【解决方案4】:

    创建变量的类和实例

    class Student:
        # Creating a Class Variables
        perc_Raise = 1.05
    
        # Creating a constructor or a special method to initialize values
        def __init__(self,firstName,lastName,marks):
            self.firstName = firstName
            self.lastName = lastName
            self.email = firstName + "." + lastName +"@northalley.com"
            self.marks = marks
    
        def fullName(self):
            return '{} {}'.format(self.firstName,self.lastName)
    
        def apply_raise(self):
            self.marks = int(self.marks * self.perc_Raise)
    

    为 Student 类创建两个实例变量

    std_1 = Student('Mahesh','Gatta',62)
    std_2 = Student('Saran','D',63)
    
    print(std_1.fullName())
    print(std_1.marks)
    
    std_1.apply_raise()
    
    print(std_1.marks)
    print(std_1.email) 
    print(std_1.__dict__)
    print(std_2.fullName())
    print(std_2.marks)
    
    std_2.apply_raise()
    
    print(std_2.marks)
    print(std_2.email)
    print(std_2.__dict__)
    
    print(Student.__dict__)
    

    继承

    class Dumb(Student):
        perc_Raise = 1.10
    
        def __init__(self,firstName,lastName,marks,prog_lang):
            super().__init__(firstName,lastName,marks)
            self.prog_lang = prog_lang
    
    std_1 = Dumb('Suresh','B',51,'Python')
    
    print(std_1.fullName())
    print(std_1.marks)
    
    std_1.apply_raise()
    
    print(std_1.marks)
    print(std_1.prog_lang)
    

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 2018-12-23
      • 2016-02-03
      • 2019-09-12
      • 2018-02-18
      • 2017-02-18
      • 2017-06-30
      相关资源
      最近更新 更多