Python/문법

파이썬(python) - 이터레이션 형 (Iteration)

snowman95 2021. 4. 18. 21:51
728x90
반응형

이터레이션 형 (Iteration)


이터레이션(Iteration) : 어떤 객체의 원소에 하나씩 차례로 접근하는 것

 - 이터러블(Iterable) : 이터레이션 가능하며 Iterator 객체로 변환가능한 객체

 - 이터레이터(Iterator) : 값을 차례대로 접근하여 조회가능한 객체

    - iter(Iterator객체) : 이터레이터 객체(자기자신) 호출

    - iter(Iterable객체) : 이터레이터 객체로 바꿔서 반환

    - next(Iterator객체) : 다음 순번을 호출. 다음 데이터 없으면 StopIteration 예외 발생

s=[1,2,3]
next(s) : 1
next(s) : 2
next(s) : 3
next(s) : StopIteration Exception

※ for문은 내부적으로 Iterable 를 Iterator로 바꿔서 순차접근 하는 것임.

    for item in iterable:

반응형