Python3 - TypeError: module .__ init __ () accepts no more than 2 arguments (3 data)

Please do not mark as duplicates, other similar questions did not solve my problem.

This is my setting

/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"

Duration:

$ python3 main.py

The only error message I get:

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)

Even if I try to use the python3 console, I got the same error message:

$python3
from actions import ViewAction

I am new to Python, but not new to programming. I assume that my error messages are related to import statements, but based on the message I cannot understand what this means.

thanks

+9
source share
4 answers

, , ( ), .

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.

+27

self, , .
: . MSeifert comment, .

0

, .

For example, if the file name is Parent1.py, and the class name is Parent, you should write

from Parent1 import Parent

However, if your file Parent1.pyis located in some folder, for example:

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

Then you have to write:

from Test.Parent1 import Parent
0
source

Creating classes and instance 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 Two Instance Variables for the 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)
-1
source

Source: https://habr.com/ru/post/1628541/


All Articles