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

PythonPython 3.xInheritanceTypeerror

Python Problem Overview


I have defined a class in a file named Object.py. When I try to inherit from this class in another file, calling the constructor throws an exception:

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

This is my code:

import Object

class Visitor(Object):
    pass

instance = Visitor()  # this line throws the exception

What am I doing wrong?

Python Solutions


Solution 1 - Python

Your error is happening because Object is a module, not a class. So your inheritance is screwy.

Change your import statement to:

from Object import ClassName

and your class definition to:

class Visitor(ClassName):

or

change your class definition to:

class Visitor(Object.ClassName):
   etc

Solution 2 - Python

Even after @Mickey Perlstein's answer and his 3 hours of detective work, it still took me a few more minutes to apply this to my own mess. In case anyone else is like me and needs a little more help, here's what was going on in my situation.

  • responses is a module
  • Response is a base class within the responses module
  • GeoJsonResponse is a new class derived from Response

Initial GeoJsonResponse class:

from pyexample.responses import Response

class GeoJsonResponse(Response):

    def __init__(self, geo_json_data):

Looks fine. No problems until you try to debug the thing, which is when you get a bunch of seemingly vague error messages like this:

> from pyexample.responses import GeoJsonResponse > ..\pyexample\responses\GeoJsonResponse.py:12: in (module) > class GeoJsonResponse(Response): > > E TypeError: module() takes at most 2 arguments (3 given) > > =================================== ERRORS ==================================== > > ___________________ ERROR collecting tests/test_geojson.py ____________________ > > test_geojson.py:2: in (module) > from pyexample.responses import GeoJsonResponse ..\pyexample\responses \GeoJsonResponse.py:12: in (module) > > class GeoJsonResponse(Response): > E TypeError: module() takes at most 2 arguments (3 given) > > ERROR: not found: \PyExample\tests\test_geojson.py::TestGeoJson::test_api_response > > C:\Python37\lib\site-packages\aenum_init_.py:163 > > (no name 'PyExample\ tests\test_geojson.py::TestGeoJson::test_api_response' in any of [])

The errors were doing their best to point me in the right direction, and @Mickey Perlstein's answer was dead on, it just took me a minute to put it all together in my own context:

I was importing the module:

from pyexample.responses import Response

when I should have been importing the class:

from pyexample.responses.Response import Response

Hope this helps someone. (In my defense, it's still pretty early.)

Solution 3 - Python

from Object import Object

or

From Class_Name import Class_name

If Object is a .py file.

Solution 4 - Python

You may also do the following in Python 3.6.1

from Object import Object as Parent

and your class definition to:

class Visitor(Parent):

Solution 5 - Python

In my case where I had the problem I was referring to a module when I tried extending the class.

import logging
class UserdefinedLogging(logging):

If you look at the Documentation Info, you'll see "logging" displayed as module.

In this specific case I had to simply inherit the logging module to create an extra class for the logging.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlobjcView Question on Stackoverflow
Solution 1 - PythonSheenaView Answer on Stackoverflow
Solution 2 - PythonSunfiShieView Answer on Stackoverflow
Solution 3 - PythonDivesh singhView Answer on Stackoverflow
Solution 4 - PythonAnthony RutledgeView Answer on Stackoverflow
Solution 5 - PythonGerardssonView Answer on Stackoverflow