Python3 - TypeError:module .__ init __()最多需要2个参数(给定3个)

时间:2016-02-12 16:19:26

标签: python python-3.x

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

这是我的设置

/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新手,但不是编程新手。我假设我的错误消息与import语句有关,但根据消息,我无法弄清楚它的含义。

由于

4 个答案:

答案 0 :(得分:27)

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

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包下的不同模块。

答案 1 :(得分:0)

当你不需要时,你正在通过self,这就是全部。
编辑:请参阅MSeifert的评论回答,因为我不想窃取内容。

答案 2 :(得分:0)

如果文件位于项目的根目录中,则可以直接输入文件名并导入。

例如,如果文件名是Parent1.py并且类名是Parent,那么您将编写

from Parent1 import Parent

例如,如果您的文件Parent1.py在任何文件夹下,则:

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

那你就得写:

from Test.Parent1 import Parent

答案 3 :(得分:-1)

创建变量的类和实例

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)
相关问题