Skip to content
Advertisement

Python – Access a list from a class using instance[]

So I have this code :

JavaScript

And when I create an instance, like this :

JavaScript

To access a value (like 3 for example), I know I should do

JavaScript

and I would like to know how to access it using

JavaScript

and still use

JavaScript

Thanks in advance !

Advertisement

Answer

Define the __getitem__ function to allow for custom indexing:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement