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?
6 Answers
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
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.)
from Object import Object
or
From Class_Name import Class_name
If Object is a .py file.
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):
Modules vs classes with basic import syntax
Errors like this most commonly happen because programmers expect Python to work like Java - where source files are expected to include exactly one "public" class named after the file, and import provides access to the class.
Python is not Java. Python does not have access modifier keywords; it allows putting any number of classes (including zero) in a source file; it also allows for things other than classes at the top level of the code; and it does not have any special behaviour for classes that share a name with the source file.
Code like import Object (assuming that the import succeeds) means that Object will be the name of a module, not a class. Python will not give special treatment to any class here even if it has the same name as the module. A hypothetical class Object defined within Object.py will be the Object attribute of the Object module: Object.Object.
Modules are not classes, and are thus not suitable as base classes.
Supposing that the Object module defines an Object class, to use that as a base class, choose one:
refer to it by that qualified name:
import Object class Visitor(Object.Object): passexplicitly alias it:
import Object my_object = Object.Object class Visitor(my_object): passuse
from ... importsyntax to assign the class directly to the desired name:from Object import Object class Visitor(Object): passor, alias it as well, using
as:from Object import Object as my_object class Visitor(my_object): pass
However, be careful with the latter syntax: