Python学習ノート4-反復器(Iterator)

794 ワード

#       for        Iterable(     )
#   next()                  Iterator(   ),Iterator         ,                ,              ,      next()         ,                  
#list,tuple(  ),dict(  ),set,str,generator(     yield generator function)  Iterable,    generator Iterator
#Iterator             , list                 
#   isinstance()         
from collections import Iterable,Iterator
isinstance([],Iterable)     #       Iterable
isinstance((x for x in range(10)),Iterator) #  generator   Iterator
#    iter()   Iterable  Iterator
isinstance(iter([]),Iterator)
#Python for        Iterable   Iterator      next()   
for x in range(5):
    print(x)
#        
temp = iter(range(5))
while True:
    try:
        print(next(temp))
    except StopIteration:
        break