TypeError:module .__ init __()最多接受2个参数(给定3个)

问题: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

我究竟做错了什么?

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?


回答 0

发生错误是因为Object是模块,而不是类。因此,您的继承权很严格。

将您的导入语句更改为:

from Object import ClassName

和您的类定义为:

class Visitor(ClassName):

要么

将您的类定义更改为:

class Visitor(Object.ClassName):
   etc

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

回答 1

即使在@Mickey Perlstein的回答和他3个小时的侦探工作之后,我仍然花了几分钟才将其应用于我自己的烂摊子。如果有人像我一样需要更多帮助,这就是我的处境。

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

初始GeoJsonResponse类:

from pyexample.responses import Response

class GeoJsonResponse(Response):

    def __init__(self, geo_json_data):

看起来不错 在尝试调试事物之前,没有问题,这是当您收到一堆看似模糊的错误消息时,如下所示:

从pyexample.responses中导入GeoJsonResponse .. \ pyexample \ responses \ GeoJsonResponse.py:12:在(模块)类GeoJsonResponse(Response)中:

E TypeError:module()最多接受2个参数(给定3个)

==================================错误=============== ======================

___________________收集测试错误/test_geojson.py ____________________

pyexample.responses中的test_geojson.py:2:在(模块)中导入GeoJsonResponse .. \ pyexample \ responses \ GeoJsonResponse.py:12:在(模块)中

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’)

错误尽了最大的努力,向我指出了正确的方向,@ Mickey Perlstein的回答仍然无效,花了我一分钟时间,将所有内容整合到我自己的上下文中:

我正在导入模块

from pyexample.responses import Response

当我应该导入该类时

from pyexample.responses.Response import Response

希望这对某人有帮助。(以我的辩护,还为时过早。)

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.)


回答 2

您也可以在Python 3.6.1中执行以下操作

from Object import Object as Parent

和您的类定义为:

class Visitor(Parent):

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):

回答 3

from Object import Object

要么

From Class_Name import Class_name

如果Object是.py文件。

from Object import Object

or

From Class_Name import Class_name

If Object is a .py file.


回答 4

在我遇到问题的情况下,当我尝试扩展类时,我指的是模块。

import logging
class UserdefinedLogging(logging):

如果查看文档信息,您将看到“日志记录”显示为模块。

在这种特定情况下,我必须简单地继承日志记录模块才能为日志记录创建一个额外的类。

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.