可能是一个 n00b 问题,但我目前有一个实现迭代器的类,所以我可以做类似的事情
Might be a n00b question, but I currently have a class that implements an iterator so I can do something like
for i in class():
但我希望能够像索引一样访问类
but I want to be able to access the class by index as well like
class()[1]
我该怎么做?
谢谢!
同时实现 __iter__()
和 __getitem__()
等方法.
Implement both __iter__()
and __getitem__()
et alia methods.
这篇关于迭代器和索引可访问的 Python 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!