__*__
System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.
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.
是的,dunder方法继承得很好。根据答案,链接的文档是Reserved classes of identifiers:
它是一个独立于
__*
类私有名称的独立类。在另一个链接的部分是Identifiers (Names),这可能更清楚:
Bold emphasis mine;以两个下划线开头、以两个下划线结尾的名称不是class private名称。在
注意两个名称类都是继承的(继承通过在类的MRO中查找属性名来实现)。名字被弄乱并不能阻止它们被继承,这就是为什么名字首先会被弄乱。通过在这些名称前面加上
_ClassName
前缀,子类可以重复使用该名称,并且自动不会冲突,因为这些子类有自己的_SubClass
前缀。在相关问题 更多 >
编程相关推荐