Python3 自定义 View 对象

标签 python python-3.x

是否可以在 Python 3 中实现自定义 View 对象?

根据文档:

The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.

那么是否有可能以某种方式创建自定义 View 对象?

我搜索了很长时间有关它的任何信息,但我找到的唯一解释是什么是 View ,而不是如何创建自定义它们如何已实现(他们的内部机制)。

最佳答案

在 Python 2 中,这些函数返回一个列表。在 Python 3 中,你得到的对象就像一个列表。如何才能像列表一样工作?

a number of 'special functions'您可以使用它来更改对象的行为。您可能已经熟悉 __init__()。对于制作“ View ”,最重要的可能是 __len__()__getitem__(),也许还有 __iter__()

关于Python3 自定义 View 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14652409/

相关文章:

python - AttributeError: 'builtin_function_or_method' 对象没有属性 'replace'

python - 与从离散数据插值的曲线相切

python 幽灵脚本 : RuntimeError: Can not find Ghostscript library (libgs)

python - AttributeError 'Nonetype' 对象没有属性。但我检查 Nonetype

python - 如何在Python中解压已使用gzip压缩的文本?

python - 具有 O(1) 随机移除和添加的数据结构,用于混洗生成器顺序

python-3.x - 查找特定行的多个列的最大值并将最大列的标题保存到新列中

python - Vim 和 python : context-unaware autocompletion of language methods

python - Virtualenv 没有安装 pip

python - 从 0 开始自动编号的枚举