Python 类型错误:module.__init__() 最多接受 2 个参数(给出 3 个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14583761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: module.__init__() takes at most 2 arguments (3 given)
提问by lobjc
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:
我在一个名为Object.py. 当我尝试从另一个文件中的此类继承时,调用构造函数会引发异常:
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?
我究竟做错了什么?
回答by Sheena
Your error is happening because Objectis a module, not a class. So your inheritance is screwy.
你的错误发生是因为Object是一个模块,而不是一个类。所以你的遗产很糟糕。
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
回答by Anthony Rutledge
You may also do the following in Python 3.6.1
您还可以在 Python 3.6.1 中执行以下操作
from Object import Object as Parent
and your class definition to:
和你的类定义:
class Visitor(Parent):
回答by SunfiShie
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.
即使在@Mickey Perlstein 的回答和他 3 小时的侦探工作之后,我仍然花了几分钟时间将其应用到我自己的烂摊子上。如果其他人像我一样需要更多帮助,这就是我的情况。
- responses is a module
- Response is a base class within the responses module
- GeoJsonResponse is a new class derived from Response
- 响应是一个模块
- 响应是响应模块中的基类
- GeoJsonResponse 是从 Response 派生的新类
Initial GeoJsonResponse class:
初始 GeoJsonResponse 类:
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 [])
from pyexample.responses import GeoJsonResponse ..\pyexample\responses\GeoJsonResponse.py:12: 在(模块)类 GeoJsonResponse(Response) 中:
E 类型错误:module() 最多接受 2 个参数(给出 3 个)
==================================== 错误 ============== ======================
____________________ 收集测试/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() 最多接受 2 个参数(给出 3 个)
错误:未找到:\PyExample\tests\test_geojson.py::TestGeoJson::test_api_response
C:\Python37\lib\site-packages\aenum__init__.py:163
(在任何 [] 中都没有名称 'PyExample\tests\test_geojson.py::TestGeoJson::test_api_response')
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:
错误正在尽最大努力为我指明正确的方向,@Mickey Perlstein 的回答已经死了,我只花了一分钟时间将它们放在我自己的上下文中:
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.)
希望这可以帮助某人。(在我看来,现在还为时过早。)
回答by Divesh Singh
from Object import Object
or
或者
From Class_Name import Class_name
If Object is a .pyfile.
如果 Object 是一个.py文件。
回答by Gerardsson
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.
在这种特定情况下,我必须简单地继承日志记录模块来为日志记录创建一个额外的类。

