12

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.

martineau
  • 99,260
  • 22
  • 139
  • 249
levacjeep
  • 628
  • 3
  • 6
  • 22
  • Did you define your ``__init__.py`` files or did you include your directories in the python path? Does the example work if you put everything in the main file? – MSeifert Feb 12 '16 at 16:41
  • 1
    Possible duplicate of [TypeError: module.\_\_init\_\_() takes at most 2 arguments (3 given)](http://stackoverflow.com/questions/14583761/typeerror-module-init-takes-at-most-2-arguments-3-given) – ShadowRanger Feb 12 '16 at 16:52
  • 1
    @ShadowRanger While the title of the "possible duplicate" question is almost identical to this one, the reasons the error is thrown in the two questions are different (i.e. the answers on that one did not help me, whereas the accepted answer here did) – Attila Jul 31 '18 at 17:21

4 Answers4

32

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.

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
  • Python cannot use relative paths so it depends how the ``__init__.py`` is setup. If he imports everything into the global ``action`` space it should work as the OP did it. If not your answer is correct. – MSeifert Feb 12 '16 at 16:45
  • 2
    This is one reason to follow the naming convention that only class names are in CamelCase, and module names should be lowercase; it would be easier to see that you aren't importing the class name where you think you are. – chepner Feb 12 '16 at 16:45
  • 1
    @MSeifert: The error the OP is getting makes it clear that the they are not doing anything clever in `__init__.py` to get that behavior; that error message is exactly what you expect to see when you try to make a class inherit from a module. – ShadowRanger Feb 12 '16 at 16:47
  • I'm coming from a Java background. So a file is not specifically a class, but a module. Gotcha. I have no __init__.py setup, I didn't know about this. – levacjeep Feb 12 '16 at 16:48
  • 1
    @levacjeep: I'm surprised it works at all then; you need at least an empty `__init__.py` for packages to work at all. Regardless, yes, Java is weird in that it only allows one (non-nested) class to be defined per source file. Python and most other OO languages can define many classes per module. Given your use case (assuming it's not complex enough to justify splitting into separate source files), you should probably just make an `actions.py` file and define both classes in it, which saves the hassle of these separate imports. – ShadowRanger Feb 12 '16 at 16:50
  • @ShadowRanger awesome thanks, trying to figure out all the conventions. This makes much more sense now. – levacjeep Feb 12 '16 at 16:51
  • Later follow-up: [PEP 420](https://www.python.org/dev/peps/pep-0420/) made `__init__.py`-less packages work just fine, which explains why this worked. I hadn't been familiar with implicit namespace packages when I wrote my last comment. – ShadowRanger Oct 02 '18 at 12:46
0

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.

Idos
  • 14,036
  • 13
  • 48
  • 65
0

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
amza
  • 806
  • 7
  • 31
Divesh singh
  • 181
  • 1
  • 9
-1

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)
Eduard Malakhov
  • 1,031
  • 4
  • 12
  • 25