Python3 custom 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 of dictionary entries, which means that when the dictionary changes, the view reflects those changes.

So is it possible to create custom view objects somehow?

I have been looking for any information about this for a very long time, but the only explanations I have found are what the views are, not how to create custom ones or how they are implemented (their internal mechanism).

+3


source to share


1 answer


In Python 2, these functions return a list. In Python 3, you get objects that act like a list. What to do, like a list?



There are several "special functions" that you can use to change the behavior of an object. You are probably already familiar with __init__()

. To create a "view", the most important are probably __len__()

, __getitem__()

and perhaps __iter__()

.

+1


source







All Articles