Answer a question

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

Running:

$ python3 main.py

The only error message I receive is:

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 for the python3 console, I received the same error message:

$python3
from actions import ViewAction

I am new to Python, but not new to programming. I'm assuming that my error messages have to do with the import statements, but based on the message I can't really figure out what it means.

Answers

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.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐