TypeError: module.__init__() 最多只接受2个参数 (3个已提供)

8 浏览
0 Comments

TypeError: module.__init__() 最多只接受2个参数 (3个已提供)

我在一个名为Object.py的文件中定义了一个类。当我尝试从另一个文件中继承这个类时,调用构造函数会抛出异常:

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

这是我的代码:

import Object
class Visitor(Object):
    pass
instance = Visitor()  # this line throws the exception

我做错了什么?

admin 更改状态以发布 2023年5月21日
0
0 Comments

即使在 @Mickey Perlstein 的答案以及他三个小时的侦探工作之后,我仍然需要几分钟来将其应用到我的混乱中。如果有人像我一样需要更多的帮助,那么这就是我所处情况的情况:

  • responses 是一个模块
  • Response 是 responses 模块中的基类
  • GeoJsonResponse 是从 Response 派生的新类

初始的 GeoJsonResponse 类:

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

看起来很好。在尝试调试时,没有任何问题,但此时您会收到一堆看似模糊的错误消息,例如:

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 [])

这些错误正在尽力指出我应该去的方向,而 @Mickey Perlstein 的答案是正确的,只是我需要一分钟才能在自己的上下文中将其全部整合在一起:

我正在导入 模块

from pyexample.responses import Response

当我应该导入

from pyexample.responses.Response import Response

希望这能帮助某些人。 (为了辩护,现在还很早。)

0
0 Comments

你的错误是因为Object是一个模块,而不是一个类。所以你的继承出了问题。

把你的导入语句改为:

from Object import ClassName

把你的类定义改为:

class Visitor(ClassName):

或者

把你的类定义改为:

class Visitor(Object.ClassName):
   etc

0