gpt4 book ai didi

python - UserDict 和 Dict 有什么区别吗?

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:08 45 4
gpt4 key购买 nike

如果我希望一个类具有字典行为,为什么要继承dictUserDict

最佳答案

你可以继承自dict在任何 2.2 版或更高版本的 Python 中,但您必须覆盖每一个感兴趣的方法——例如,您对 __getitem__ 的覆盖get使用除非你也覆盖那个,依此类推。

UserDict.DictMixin mix-in 可以追溯到更远的地方,它只允许您实现几个方法:由 mix-in 提供的其他方法选取并使用您自己的覆盖。但是请注意,来自 the docs :

Starting with Python version 2.6, it is recommended to use collections.MutableMapping instead of DictMixin.

collections 中的新 ABC(抽象基类)模块具有与旧的 UserDict.DictMixin 相同的优点,适用性更广,规律性更强。

关于python - UserDict 和 Dict 有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2083504/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com