Is it possible to implement custom view objects in Python 3?
According to the documentation:
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.
So is it possible to somehow create custom view objects?
I was searching very long any information about it, but the only explanations I found are what are the views, not how to create custom or how they are implemented (their internal mechanism).
In Python 2, those functions return a list. In Python 3, you get objects that act like a list. What does it take to act like a list?
There are a number of 'special functions' you can use to change an object's behavior. You're probably familiar with __init__()
already. For making a 'view', the most important are probably __len__()
, __getitem__()
and maybe __iter__()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With