简体   繁体   English

Python继承和调用父类构造函数

[英]Python inheritance and calling parent class constructor

This is what I'm trying to do in Python: 这就是我在Python中尝试做的事情:

class BaseClass:
    def __init__(self):
        print 'The base class constructor ran!'
        self.__test = 42

class ChildClass(BaseClass):

    def __init__(self):
        print 'The child class constructor ran!'
        BaseClass.__init__(self)

    def doSomething(self):
        print 'Test is: ', self.__test


test = ChildClass()
test.doSomething()

Which results in: 结果如下:

AttributeError: ChildClass instance has no attribute '_ChildClass__test'

What gives? 是什么赋予了? Why doesn't this work as I expect? 为什么这不能像我期望的那样工作?

From python documentation: 从python文档:

Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. 专用名称修改:当在类定义中文本方式出现的标识符以两个或多个下划线字符开头且不以两个或多个下划线结尾时,它将被视为该类的私有名称 Private names are transformed to a longer form before code is generated for them. 在为其生成代码之前, 私有名称转换为更长的形式 The transformation inserts the class name in front of the name , with leading underscores removed, and a single underscore inserted in front of the class name. 转换在名称前插入类名,删除前导下划线,并在类名前面插入单个下划线。 For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam . 例如,名为Ham的类中出现的标识符__spam将转换为_Ham__spam This transformation is independent of the syntactical context in which the identifier is used. 此转换独立于使用标识符的语法上下文。 If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. 如果转换后的名称非常长(超过255个字符),则可能会发生实现定义的截断。 If the class name consists only of underscores, no transformation is done. 如果类名仅由下划线组成,则不进行转换。

So your attribute is not named __test but _BaseClass__test . 因此,您的属性不是__test而是_BaseClass__test

However you should not depend on that, use self._test instead and most python developers will know that the attribute is an internal part of the class, not the public interface. 但是你不应该依赖它,使用self._test而大多数python开发人员都知道该属性是类的内部部分,而不是公共接口。

You could use Python's introspection facilities to get you the information you are looking for. 您可以使用Python的内省工具来获取您正在寻找的信息。 A simple dir(test) will give you 一个简单的目录(测试)会给你

['_BaseClass__test', '__doc__', '__init__', '__module__', 'doSomething']

Note the '_BaseClass__test'. 注意'_BaseClass__test'。 That's what you're looking for. 这就是你要找的东西。

Check this for more information. 请查看以获取更多信息。

Double underscored variables can be considered "private". 双下划线变量可以被视为“私有”。 They are mangled with the class name, amongst other to protect multiple baseclasses from overriding eachothers members. 它们被类名称破坏,其中包括保护多个基类不会覆盖每个成员。

Use a single underscore if you want your attribute to be considered private by other developers. 如果您希望其他开发人员将您的属性视为私有,请使用单个下划线。

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

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