Search code examples
pythonclassiterator

Class accessible by iterator and index


My class implements an iterator so I can do:

for i in class():

But I want to be able to access the class by index as well:

class()[1]

How can I do that?


Solution

  • Implement both __iter__() and __getitem__() et alia methods.