generator, yield, iterable, iterator

generator

  • A function which returns an iterator. It looks like a normal function except that it contains yield statements for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function. Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator resumes, it picks-up where it left-off (in contrast to functions which start fresh on every invocation).

  • iterator한 요소를 return하는 함수

  • yield란 키워드를 사용하며, next()란 함수로 다음 값을 나타낼 수 있음
  • 일반적인 함수는 사용이 종료되면 결과값을 호출부로 반환한 후, 함수 자체를 종료시키고 메모리상에서 clear
  • generator 함수가 실행 중 yield가 존재하면, 해당 함수는 그 상태로 정지되고 반환 값을 next() 호출한 쪽으로 전달. => local 변수나 instruction pointer 등과 같이 함수 내부에서 사용된 데이터들이 메모리에 그대로 유지됨

  • 메모리를 효율적으로 관리할 수 있어, 대용량 처리 함수를 제작할 때 용이함

  • list comprehension 형태에서 [] 대신 ()을 사용하면 generator가 생성됨

In [16]:
(i for i in range(1000) if i % 2)


Out[16]:
<generator object <genexpr> at 0x110529780>

In [13]:
import sys

In [14]:
sys.getsizeof( [i for i in range(1000) if i % 2] )


Out[14]:
4272

In [15]:
sys.getsizeof( (i for i in range(1000) if i % 2) )


Out[15]:
88

In [2]:
def generator(n):
    i = 0
    while i < n:
        yield i
        i += 1

iterable

  • list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an iter() or getitem() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ...). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.

  • list, str, tuple 같이 순회할 수 있는 객체

  • iter() 나 getitem() 메소드로 정의된 class 는 모두 iterable

iterate

  • An object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again. Iterators are required to have an iter() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

  • next() 메소드로 데이터를 순차적으로 호출 가능한 object

  • iterable 이라고 해서 반드시 iterator 라는 것은 아님
  • iter()를 사용하면 iterable한 객체를 iterator로 만들 수 있음
  • list나 tuple을 for문 돌리는 경우엔 iter를 정의하지 않아도 순차적으로 접근이 가능한 것은 python 내부에서 list를 임시적으로 iterator로 변환해줬기 때문-!

In [3]:
x = [1,2,3]

In [4]:
x.next()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-e05f366da090> in <module>()
----> 1 x.next()

AttributeError: 'list' object has no attribute 'next'

In [5]:
type(x)


Out[5]:
list

In [6]:
y = iter(x)

In [7]:
type(y)


Out[7]:
list_iterator

In [9]:
next(y)


Out[9]:
1

In [10]:
next(y)


Out[10]:
2

In [11]:
next(y)


Out[11]:
3

In [12]:
next(y)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-12-714521682ae9> in <module>()
----> 1 next(y)

StopIteration: 

In [ ]: