roberthsu2003 / __2024_04_17_mon_wed__

Python與AI人工智慧開發入門
24 stars 5 forks source link

關於 __iter__ 與 __getitem__ 的功能 #22

Open chihweihan opened 4 months ago

chihweihan commented 4 months ago

9999

老師您好,關於講義範例中

itergetitem

在程式內的功能是什麼?

roberthsu2003 commented 4 months ago

9999

老師您好,關於講義範例中

itergetitem

在程式內的功能是什麼?

#實作for_in迴圈,__iter__(self),必需傳出interator物件,使用iter()會傳出iter的物件
def __iter__(self):
        return iter(self.root)

#實作subscript[]
def __getitem__(self, item):
        return self.root[item]