반응형
/************************************************************************************************
-- Title : [PY3.3] 함수에서 이터레이터(iterator) & 제너레이터(Generator)
-- Reference : 빠르게 활용하는 파이썬3 프로그래밍
-- Key word : 파이썬 python iterator 이터레이터 함수 function
************************************************************************************************/
#-- 이터레이터: 반복처
>>> s='abc'
>>> it=iter(s)
>>> it
<str_iterator object at 0x02265030>
>>> next(it)
'a'
>>> next(it)
'b'
>>> it.__next__()
'c'
>>> next(it)
Traceback (most recent call last):
  File "<pyshell#227>", line 1, in <module>
    next(it)
StopIteration

#-- 제너레이터(return 대신 yield사용하여 함수 개체를 유치한 체 값 리턴)
>>> def reverse(data):
            for index in range(len(data) -1, -1, -1):
                        yield data[index]
  
>>> for char in reverse('golf'):
            print(char)
 
f
l
o
g
>>> def abc():
            data="abc"
            for char in data:
                        yield char
  
>>> abc
<function abc at 0x02319AE0>
>>> abc()
<generator object abc at 0x022FB530>
>>> it=iter(abc())
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'





반응형

+ Recent posts