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

I defined a class in a file called Object.py . When I try to inherit 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?

+129
python
Jan 29 '13 at 13:12
source share
3 answers

Your error occurs because Object is a module, not a class. So your inheritance is shy.

Change the import statement:

 from Object import ClassName 

and the definition of your class:

 class Visitor(ClassName): 

or

change the definition of your class to:

 class Visitor(Object.ClassName): etc 
+212
Jan 29 '13 at 14:07
source share

You can also do the following in Python 3.6.1

 from Object import Object as Parent 

and the definition of your class:

 class Visitor(Parent): 
+3
May 9 '17 at 15:47
source share

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

  • Reviews is a module
  • The response is the base class in the response module.
  • GeoJsonResponse is a new class derived from Response

GeoJsonResponse starter class:

 from pyexample.responses import Response class GeoJsonResponse(Response): def __init__(self, geo_json_data): 

Looks good. No problem until you try to debug this thing when you get a bunch of seemingly vague error messages like this:

from pyexample.responses import GeoJsonResponse .. \ pyexample \ response \ GeoJsonResponse.py: 12: into the (module) class GeoJsonResponse (Response):

E TypeError: module () accepts no more than 2 arguments (given 3)

==================================== ERRORS ============== ========================

___________________ ERROR collecting tests /test_geojson.py ____________________

test_geojson.py:2: in (module) from pyexample.responses import GeoJsonResponse .. \ pyexample \ response \ GeoJsonResponse.py: 12: in (module)

Class GeoJsonResponse (Response): E TypeError: module () accepts no more than 2 arguments (3 given)

ERROR: not found: \ PyExample \ tests \ test_geojson.py :: TestGeoJson :: test_api_response

C:. \ Python37 \ Lib \ site packages \ aenum__init __ ne: 163

(without the name 'PyExample \ tests \ test_geojson.py :: TestGeoJson :: test_api_response' in any of [])

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

I imported the module :

 from pyexample.responses import Response 

when i had to import the class :

 from pyexample.responses.Response import Response 

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

0
Apr 12 '19 at 11:16
source share



All Articles