>>> f=open('script2.py')
>>> f
<_io.TextIOWrapper name='script2.py' mode='r' encoding='UTF-8'>
>>> iter(f)
<_io.TextIOWrapper name='script2.py' mode='r' encoding='UTF-8'>
f
和iter(f)
类型相同,因为file object是它自身的iterator。
`
f.next()
'import sys '
`
>>> L=[1,2,3]
>>> iter(L)
>>> L.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__next__'
list则不是它自身的iterator,所以不能直接使用__next__()
,必须使用一次iter()
函数:
>>> L=[1,2,3]
>>> I=iter(L)
>>> I.__next__()
1