Python类可以通过迭代器和索引访问 [英] Python class accessible by iterator and index
本文介绍了Python类可以通过迭代器和索引访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能是一个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类可以通过迭代器和索引访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文