python类/子类inheritan背后的基本原理

2024-06-16 19:55:27 发布

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

当我创建如下所示的父类和子类时,为什么父类中的参数不被子类自动拉入?在

我明白显式更好,但是我想知道在什么情况下这个代码。。。在

class testParent(object):
    def __init__(self,testParentParam1,testParentParam2):
        pass


class testChild(testParent):
    def __init__(self,testParentParam1,testParentParam2,testChildParam1,testChildParam2):
        pass

比这个代码好。。。在

^{pr2}$

Tags: 代码self参数objectinitdef情况pass
1条回答
网友
1楼 · 发布于 2024-06-16 19:55:27

派生类扩展基类。他们可能需要更多的信息。考虑:

class BaseTextDocument(object):
    def __init__(self, content):
        self.content = content

class WordDocument(object):
    def __init__(self, path, word_version="guess_from_file"):
        content = parse_word_document(path, word_version)
        super(WordDocument, self).__init__(content)

相关问题 更多 >