尝试用 python 测试继承。我的场景如下..
在一个文件夹中,我有一个名为 Asset_Base 的基类和一个名为 Asset 的子类,它们如下所示。
在 Asset_Base.py
文件中:
class Asset_Base(object):
def __init__(self):
pass
def sayHello(self):
print('Hello!')
在 Asset.py
文件中:
import Asset_Base
class Asset(Asset_Base):
def __init__(self):
pass
def sayHello(self):
super().sayHello()
a = Asset()
a.sayHello()
当我运行这个 Assets 类时得到这个错误..
class Asset(Asset_Base):
TypeError: module.__init__() takes at most 2 arguments (3 given)
在尝试了一些东西后发现如果我像下面这样更改导入语句就可以正常工作
从 Asset_Base 导入 *
我是 python 的新手,不确定
import Asset_Base
和from Asset_Base import *
谁能解释一下。
提前致谢。
最佳答案
那是因为在您的第一个示例中,您的类继承自 Asset_Base
模块(即 .py 文件)——不是 它包含的同名类。
请注意您的错误消息是如何谈论 module.__init__()
的。
更改您的导入语句,使其读取from Asset_Base import Asset_Base
。
此外,“模块包含一个同名的类”在 Python 中是一种反模式。避免这样做。
关于python - 面对python继承中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27042721/