子类 dict:UserDict、dict 还是 ABC? [英] Subclass dict: UserDict, dict or ABC?

查看:59
本文介绍了子类 dict:UserDict、dict 还是 ABC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UserDictdictABC 有什么区别,推荐哪个?文档似乎不赞成 UserDict?

What's the difference between UserDict, dict and ABC and which one is recommended? The docs seem to deprecate UserDict?

似乎 UserDict 的 update() 会使用我的 setitem 方法而 dict 不会?考虑到我想要自定义 setitemgetitem 函数,哪些方法真的很重要?

Also it seems UserDict's update() would use my setitem method whereas dict doesn't? Which methods are really essential to override given I want custom setitem and getitem function?

使用 ABC 时,我必须完全实现所有方法,因为它不提供默认实现?

With ABCs I'd have to implement absolutely all methods since it provides no default implementation?

我想做一个dict来做两件事:

I want to make a dict that does two things:

  • intern() 所有的键和值
  • 将一些值存储在 SQLite 数据库中

那么UserDictdictABC 中的哪一个最适合我这样做?

So which of UserDict, dict and ABC would best allow me to do this?

推荐答案

如果你想要一个实际保存数据的自定义集合,子类 dict.如果您想扩展接口(例如,添加方法),这尤其有用.

If you want a custom collection that actually holds the data, subclass dict. This is especially useful if you want to extend the interface (e.g., add methods).

不过,所有内置方法都不会调用您的自定义 __getitem__/__setitem__.如果您需要完全控制这些,请创建一个实现 collections.MutableMapping 抽象基类的自定义类.

None of the built-in methods will call your custom __getitem__ / __setitem__, though. If you need total control over these, create a custom class that implements the collections.MutableMapping abstract base class instead.

ABC 不提供存储实际数据的方法,只提供了一些方法的默认实现的接口.但是,这些默认实现将调用您的自定义 __getitem____setitem__.您必须使用内部 dict 来保存数据,并实现所有抽象方法:__len____iter____getitem____setitem____delitem__.

The ABC does not provide a means to store the actual data, only an interface with default implementations for some methods. These default implementations will, however, call your custom __getitem__ and __setitem__. You will have to use an internal dict to hold the data, and implement all abstract methods: __len__, __iter__, __getitem__, __setitem__, and __delitem__.

collections 模块中的 UserDict 类(在 Python 2 中,该模块也称为 UserDict)是内部dict,实现 MutableMapping ABC.如果您想自定义 dict 的行为,此实现可能是一个起点.

The class UserDict from the collections module (in Python 2, the module is called UserDict as well) is a wrapper around an internal dict, implementing the MutableMapping ABC. If you want to customize the behavior of a dict, this implementation could be a starting point.

总结:

  • MutableMapping 定义接口.将其子类化以创建类似于 dict 的东西.是否以及如何存储数据完全取决于您.
  • UserDictMutableMapping 的实现,使用内部真实"dict 作为存储.如果您想要一个类似 dict 的存储集合,但要覆盖 dict 公开的一些方法,这对您来说可能是一个很好的起点.但请务必阅读代码以了解基本方法是如何实现的,以便在覆盖方法时保持一致.
  • dict 是真实的东西".如果您想扩展接口,则将其子类化.覆盖执行自定义操作的方法可能很危险,因为通常有多种访问数据的方式,并且最终可能会导致 API 不一致.
  • MutableMapping defines the interface. Subclass this to create something that acts like a dict. It's totally up to you if and how you store the data.
  • UserDict is an implementation of MutableMapping using an internal "real" dict as storage. If you want a dict-like storage collection but override some methods exposed by dict, this might be a good starting point for you. But make sure to read the code to know how the basic methods are implemented, so that you are consistent when overriding a method.
  • dict is "the real thing". Subclass this if you want to extend the interface. Overriding methods to do custom things might be dangerous, as there are usually multiple ways of accessing the data, and you could end up with an inconsistent API.

这篇关于子类 dict:UserDict、dict 还是 ABC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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