TypeError: modu .__init__()最多接受2个参数(3个给定参数)

时间:2022-06-01 23:18:41

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?

我做错了什么?

2 个解决方案

#1


148  

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

#2


1  

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

#1


148  

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

#2


1  

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