importing module causes TypeError: module.__init__() takes at most 2 arguments (3 given)

前端 未结 4 745
醉梦人生
醉梦人生 2021-01-11 13:45

Please don\'t mark as duplicate, other similar questions did not solve my issue.

This is my setup

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


        
相关标签:
4条回答
  • 2021-01-11 14:33

    Your imports are wrong, so you're trying to inherit from the modules themselves, not the classes (of the same name) defined inside them.

    from actions import ListitAction
    

    in ViewAction.py should be:

    from actions.ListitAction import ListitAction
    

    and similarly, all other uses should switch to explicit imports of from actions.XXX import XXX (thanks to the repetitive names), e.g. from actions import ListitAction, ViewAction must become two imports:

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

    because the classes being imported come from different modules under the actions package.

    0 讨论(0)
  • 2021-01-11 14:39

    Creating classes and instance of variables

    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)
    

    Creating a two instance variable for a Student class

    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__)
    

    Inheritance

    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)
    
    0 讨论(0)
  • 2021-01-11 14:40

    You are passing self when you don't need to, that's all.
    Edit: see MSeifert's comment answer since I don't want to steal content.

    0 讨论(0)
  • 2021-01-11 14:41

    If your file is in the root directory of the project then you can directly write the file name and import.

    For example, if filename is Parent1.py and class name is Parent, then you would write

    from Parent1 import Parent
    

    However, if your file Parent1.py is under any folder for example:

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

    Then you would have to write:

    from Test.Parent1 import Parent
    
    0 讨论(0)
提交回复
热议问题