简体   繁体   English

python类/子类继承背后的基本原理

[英]rationale behind python class/subclass inheritance

When I create a parent class and child class as shown below, why don't the arguments from the parent class automatically get pulled in by the child class? 当我创建一个父类和子类,如下所示,为什么父类的参数不会被子类自动拉入?

I understand that explicit is better, but I'm wondering in what circumstance this code... 我明白显性更好,但我想知道这个代码在什么情况下......

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


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

Is better than this code... 比这个代码更好......

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


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

Derived classes extend base classes. 派生类扩展基类。 That means they might need more/less/different information at construction time to do their extending. 这意味着他们可能在施工时需要更多/更少/不同的信息来进行扩展。 Consider: 考虑:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM