迭代器和索引可访问的 Python 类 [英] Python class accessible by iterator and index

查看:21
本文介绍了迭代器和索引可访问的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆