python Inheritan中面临的问题

2024-10-01 13:43:29 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试用python测试继承性。我的设想如下。。在

在一个文件夹中,我有一个名为Asset\u base的基类和一个名为Asset的子类,它们如下所示。

Asset_Base.py文件中:

class Asset_Base(object):
    def __init__(self):
        pass

    def sayHello(self):
        print('Hello!')

Asset.py文件中:

^{pr2}$

当我运行这个资产类时,得到这个错误。。在

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

在尝试了几件事后发现,如果我只更改下面的import语句,效果很好

from Asset_Base import *

我是python新手,不知道这两者之间的区别

import Asset_Base和{}

谁能给我解释一下。

提前谢谢。


Tags: 文件pyimportself文件夹baseinitdef
2条回答

这是因为在第一个示例中,您的类继承了Asset_Base模块(即.py文件)而不是它包含的同名类。在

请注意您的错误消息是如何谈论module.__init__()。在

更改import语句,使其读为from Asset_Base import Asset_Base。在

另外,“module contains a class of same name”是Python中的反模式。避免这样做。在

import Asset_Base

导入模块(文件)

^{pr2}$

导入文件中的所有内容(在本例中为Asset\u Base类)

有关更多信息,请查看Pythondocumentation。在

相关问题 更多 >