python iterator

python iterator

Concepts1

An object some time is just the iterator:

>>> f = open('script2.py')
>>> iter(f) is f
True
>>> iter(f) is f.__iter__()
True
>>> f.__next__()
'import sys\n

While some other are not:

>>> L = [1, 2, 3]
>>> iter(L) is L
False
>>> L.__next__()
AttributeError: 'list' object has no attribute '__next__'
>>> I = iter(L)
>>> I.__next__()
1
>>> next(I)
2

Notes and Reference


  1. Learning python 5e by Mark lutz, Page 421. ↩

你可能感兴趣的:(#python)