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.
In versions of Python prior to 2.2,
you could not directly subclass
built-in datatypes like strings,
lists, and dictionaries. To compensate
for this, Python comes with wrapper
classes that mimic the behavior of
these built-in datatypes: UserString,
UserList, and UserDict. Using a
combination of normal and special
methods, the UserDict class does an
excellent imitation of a dictionary.
In Python 2.2 and later, you can
inherit classes directly from built-in
datatypes like dict.
你说得对:
注意第一句话。这来自UserDict的documentation。在
哦,在Python3中它不见了。在
包装器类已经从Python3中删除了,因为它们已经有一段时间没有那么有用了。mixin类,
UserDict.DictMixin
是一个完全不同的故事,它的有用特性现在在collections
模块(python2.6和3.*)中的“抽象基类”中随处可见。在我在你链接的页面上找到了一个关于答案的提示:
实际上,今天您可能希望将dict子类,而不是UserDict。在
相关问题 更多 >
编程相关推荐