是否可以在 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/