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

标签 python inheritance types wrapper built-in

我正在阅读 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 中它消失了。

关于python - 直接从内置类型继承与其在 Python 中的包装类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659337/

相关文章:

Haskell 类型过于宽松 : need to apply continuation at most once

java - 继承和递归数据结构(树): identical child class with one method removed behaves differently

java - 这在 Java : Map<SomeObject, Map<SomeOtherObject>> 中可能吗?

python - 单独使用 while 循环的素数生成器的逻辑错误

python - pandas.Series.explode 抛出 AttributeError

python - 将使用 pandas group by 计算的总和应用于组的所有元素

Python 记录 FileHandler 输出空白文件

java - 如何扩展使用 FXML 的自定义 JavaFX 组件

C++ 将强类型基类与 CRTP 和返回值类型推导混合

c++ - C++中类型信息什么时候倒流?