本文介紹了迭代器和索引可訪問的 Python 類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
可能是一個 n00b 問題,但我目前有一個實現(xiàn)迭代器的類,所以我可以做類似的事情
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]
我該怎么做?
謝謝!
推薦答案
同時實現(xiàn) __iter__()
和 __getitem__()
等方法.
Implement both __iter__()
and __getitem__()
et alia methods.
這篇關于迭代器和索引可訪問的 Python 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!