Python中UserDict类的优点 [英] Advantages of UserDict class in Python

查看:88
本文介绍了Python中UserDict类的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 UserDict 类有什么优势?

我的意思是,我真正得到的是,而不是

I mean, what I really get if instead of

class MyClass(object):
    def __init__(self):
        self.a = 0
        self.b = 0
...
m = MyClass()
m.a = 5
m.b = 7

我将写以下内容:

class MyClass(UserDict):
    def __init__(self):
        UserDict.__init__(self)
        self["a"] = 0
        self["b"] = 0
...
m = MyClass()
m["a"] = 5
m["b"] = 7

编辑:如果我理解正确,那么在两种情况下都可以在运行时中向对象添加新字段吗?

Edit: If I understand right I can add new fields to an object in a runtime in both cases?

m.c = "Cool"

m["c"] = "Cool"

推荐答案

UserDict.UserDict 自Python 2.2起没有实质性的附加值,因为正如@gs所提到的,您现在可以直接将dict子类化-它仅存在于与Python 2.1及更低版本的向后兼容中,而内置类型无法实现.子类.尽管如此,它还是作为collections模块中的适当位置). UserDict"rel =" noreferrer>文档现在提到,

UserDict.UserDict has no substantial added value since Python 2.2, since, as @gs mention, you can now subclass dict directly -- it exists only for backwards compatibility with Python 2.1 and earlier, when builtin types could not be subclasses. Still, it was kept in Python 3 (now in its proper place in the collections module) since, as the docs now mention,

对此类的需求已经 被以下能力部分取代 直接从dict继承子类;然而, 这节课可以更容易地使用 因为基础字典是 可作为属性访问.

The need for this class has been partially supplanted by the ability to subclass directly from dict; however, this class can be easier to work with because the underlying dictionary is accessible as an attribute.

UserDict.DictMixin,在Python 2中非常方便-正如文档所述,

UserDict.DictMixin, in Python 2, is quite handy -- as the docs say,

该模块定义了一个mixin DictMixin, 定义所有的字典方法 已经有最低限度的课程 映射接口.这个很大 简化需要编写的类 可以代替字典 (例如货架模块).

The module defines a mixin, DictMixin, defining all dictionary methods for classes that already have a minimum mapping interface. This greatly simplifies writing classes that need to be substitutable for dictionaries (such as the shelve module).

将其子类化,定义一些基本方法(至少__getitem__,这对于不具有获取键或迭代能力的只读映射就足够了;如果需要这些功能,还可以keys;可能是__setitem__ ,并且您具有R/W映射,但无法删除项目;添加__delitem__以获得完整功能,并可能出于性能原因而覆盖其他方法),并获得dict丰富API的完整实现(updateget等). 模板方法设计模式的一个很好的例子.

You subclass it, define some fundamental methods (at least __getitem__, which is sufficient for a read-only mapping without the ability to get keys or iterate; also keys if you need those abilities; possibly __setitem__, and you have a R/W mapping without the ability of removing items; add __delitem__ for full capability, and possibly override other methods for reasons of performance), and get a full-fledged implementation of dict's rich API (update, get, and so on). A great example of the Template Method design pattern.

在Python 3中,DictMixin不见了;您可以通过依赖collections.MutableMapping来获得几乎相同的功能(或者对于R/O映射,只需collections.Mapping即可).它虽然没有那么方便,但还是有点优雅(请参阅此问题,该文档以"won"结束了修复";简短的讨论值得一读).

In Python 3, DictMixin is gone; you can get almost the same functionality by relying on collections.MutableMapping instead (or just collections.Mapping for R/O mappings). It's a bit more elegant, though not QUITE as handy (see this issue, which was closed with "won't fix"; the short discussion is worth reading).

这篇关于Python中UserDict类的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆