Skip to main content
 首页 » 编程设计

python之直接从内置类型继承与其在 Python 中的包装类

2025年05月04日15qlqwjy

我正在阅读 Dive Into Python由 Mark Pilgrim 撰写,并已进入继承部分。在 section 5.5 , Pilgrim 提到了从包装类 UserDict 继承与从内置 dict 类型继承之间的区别。

我无法理解为什么有人会为包装类而烦恼...从 UserDict 包装类(或任何其他 UserXxx 类)继承有什么好处?

非常感谢您的意见。谢谢!

请您参考如下方法:

你是对的:

The need for this class has been largely supplanted by the ability to subclass directly from dict (a feature that became available starting with Python version 2.2). Prior to the introduction of dict, the UserDict class was used to create dictionary-like sub-classes that obtained new behaviors by overriding existing methods or adding new ones.

注意第一句话。这来自 documentation用户字典。

哦,在 Python 3 中它消失了。