, __iter __() , , for-loop , iter() , . __iter __() for-loops. iter(), __next __() .
:
1) __iter __(), .
2) __next __(), , , StopIteration .
3) __iter __(), self. , .
, __iter __(), self, , :
>>> s = 'hello world'
>>> it = iter(s)
>>> next(it)
'h'
>>> next(it)
'e'
>>> list(it)
['l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
, , :
>>> s = 'hello world'
>>> it = iter(s)
>>> list(zip(it, it))
[('h', 'e'), ('l', 'l'), ('o', ' '), ('w', 'o'), ('r', 'l')]
:
1) - __getitem __(), IndexError. str Python 2.
2) , , . , next() . , ( , ).
3) , , Python. : https://en.wikipedia.org/wiki/Iterator_pattern