Skip to main content
 首页 » 编程设计

python之`.__mro__` 和 `.mro()` 是 CPython 实现细节吗

2024年10月01日18zhenyulu

python type.mro()记录在 section 4.13. Special Attributes 中介绍:

The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the dir() built-in function.

这是否意味着这些 CPython 实现细节可能不会出现在其他 Python 实现中?

请您参考如下方法:

不,这些是 python 对象模型的一部分。您可以相信它们存在于符合引用实现的其他 python 实现中。请注意,该页面特别指出可以覆盖 class.mro 以自定义类的 __mro__。

另请注意 inspect.getmro可用于所有 python 实现,并且 __mro__ 属性也在 data model page 中提到.