Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 custom view objects

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).

like image 307
Wojciech Danilo Avatar asked Nov 12 '22 12:11

Wojciech Danilo


1 Answers

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__().

like image 56
Sean McSomething Avatar answered Nov 15 '22 06:11

Sean McSomething