尝试用python测试继承性。我的设想如下。。在
在一个文件夹中,我有一个名为Asset\u base的基类和一个名为Asset的子类,它们如下所示。
在Asset_Base.py
文件中:
class Asset_Base(object):
def __init__(self):
pass
def sayHello(self):
print('Hello!')
在Asset.py
文件中:
当我运行这个资产类时,得到这个错误。。在
class Asset(Asset_Base):
TypeError: module.__init__() takes at most 2 arguments (3 given)
在尝试了几件事后发现,如果我只更改下面的import语句,效果很好
from Asset_Base import *
我是python新手,不知道这两者之间的区别
import Asset_Base
和{
谁能给我解释一下。
提前谢谢。
这是因为在第一个示例中,您的类继承了
Asset_Base
模块(即.py文件)而不是它包含的同名类。在请注意您的错误消息是如何谈论
module.__init__()
。在更改import语句,使其读为
from Asset_Base import Asset_Base
。在另外,“module contains a class of same name”是Python中的反模式。避免这样做。在
导入模块(文件)
^{pr2}$导入文件中的所有内容(在本例中为Asset\u Base类)
有关更多信息,请查看Pythondocumentation。在
相关问题 更多 >
编程相关推荐