Python for循環與__getitem__的關系記錄


一個類里面如果有__iter__,for循環就是找它取,沒有的話就會找__getitem__。

如果自己定義類的話,我自己覺的還是先定義好__iter__比較好,因為如果調用__getitem__來執行循環讀取速度也應該不會快,而且邏輯理解更加復雜。

前面一筆看過沒有留心具體的執行情況。

In [169]: class Foo: 
     ...:     def __getitem__(self, pos): 
     ...:         print(pos) 
     ...:         return range(10)[pos] 
     ...:     

 

In [172]: for i in f: 
     ...:     ... 
     ...:      
     ...:                                                                                          
0
1
2
3
4
5
6
7
8
9
10

當一個for循環或者list,tuple去讀取該對象的數據時,

將會啟動一個whileTrue的死循環,會從0開始迭代讀取+1讀取數據並根據return 返回。

如果沒有設置StopIteration或者IndexError錯誤來停止循環,這是一個無法停下來的循環。

從代碼可以看出,如果沒有報錯或者設置顯式的條件,這個for循環會無線循環。

我現在設置一個顯式的設置。

In [173]: class Foo: 
     ...:     def __getitem__(self, pos): 
     ...:         if pos >5: 
     ...:             raise StopIteration 
     ...:         print(pos) 
     ...:         return range(10)[pos] 
     ...:          

 

In [177]: for i in f: 
     ...:     ... 
     ...:                                                                                          
0
1
2
3
4
5

 

將錯誤設置為IndexError也可以執行,但TypeError就不行了。

     ...:     def __getitem__(self, pos): 
     ...:         if pos >5: 
     ...:             raise IndexError 
     ...:         print(pos) 
     ...:         return range(10)[pos] 
     ...:                                                                                          

In [182]:                                                                                          

In [182]: f = Foo()                                                                                

In [183]: for i in f: 
     ...:     ... 
     ...:                                                                                          
0
1
2
3
4
5

 

如果用list去運行這個參數會把返回的一個一個元素,裝入列表當中:

In [184]: list(f)                                                                                  
0
1
2
3
4
5
Out[184]: [0, 1, 2, 3, 4, 5]

 

只有__getitem__的類的實例是屬於可迭代對象,但用isinstances測試collections.Iterable是不能通過的,書后面介紹可以通過iter函數來測試,如果沒報錯就說明是可迭代對象,然后生成一個沒有__next__屬性的迭代器。

 

In [185]: from collections import Iterable                                                         

In [186]: isinstance(f, Iterable)                                                                  
Out[186]: False

In [187]: iter(f)                                                                                  
Out[187]: <iterator at 0x114f2be50>

 

 

 dir(f)                                                                                   
Out[189]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM