Advanced Python #2 Iterators

강두연·2020년 11월 24일
0

Advanced Python

목록 보기
2/2


Iterator란 값을 순차적으로 꺼내올 수 있는 객체이다.

mylist 라는 list가 있을때 for 문으로 값을 하나씩 프린트하는 코드는 다음과 같다.

for n in mylist:
	print(n)
 
1
2
3
4
5

반복가능한 객체인지 확인해보는 방법은 dir로 호출하여 __iter__ 함수가 있는지 확인해 볼 수 있다.
그리고 직접 __iter__ 함수를 출력한 것을 print문을 사용해서 출력해보면 이터레이터 객체임을 확인할 수 있다.

>>>> print(dir(mylist))
['__add__', '__class__', '__contains__', 
'__delattr__', '__delitem__', '__dir__', 
'__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__getitem__', 
'__gt__', '__hash__', '__iadd__', 
'__imul__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', 
'__lt__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', 
'__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'append', 'clear', 
'copy', 'count', 'extend', 'index', 
'insert', 'pop', 'remove', 'reverse', 'sort']

>>> print(mylist.__iter__())
<list_iterator object at 0x7fac601f7400>

해당 이터레이터 객체를 dir로 출력해보면 __next__ 함수가 들어있는 것을 확인할 수 있고 __next__ 함수는 다음 요소를 하나씩 꺼내오는 함수다.

>>> print(dir(mylist.__iter__()))
['__class__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', 
'__length_hint__', '__lt__', '__ne__', '__new__', '__next__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__setstate__', '__sizeof__', '__str__', '__subclasshook__']

>>> iter_mylist = mylist.__iter__()
>>> iter_mylist.__next__()
1
>>> iter_mylist.__next__()
2
>>> iter_mylist.__next__()
3
>>> iter_mylist.__next__()
4
>>> iter_mylist.__next__()
5
>>> iter_mylist.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

위와 같이 이터레이터 객체를 변수에 담아서 __next__()를 하게 되면 리스트에 있는 순서대로 하나씩 값을 반환한다.
마지막 값을 반환하고 익덱스를 벗어나서 가져올 값이 없으면 StopIteration이라는 에러를 반환해준다.

그럼 이제 파이썬 기본함수는 중에 앞에서 알아본 __iter__함수와 __next__함수를 편하게 사용하게 해주는 iter와 next에 대하여 알아보자.
iter는 객체의 __iter__ 함수를 호출해주고 next는 객체의 __next__ 함수를 호출하는 함수이다. 위의 mylist 리스트에 iter와 next를 적용해서 for loop 으로 출력하는 것을 while문으로 구현해보자.

>>> myiter = iter(mylist)
while True:
    try:
     	print(next(myiter))
	except StopIteration:
		break 
1
2
3
4
5

딕셔너리 또한 반복가능한 객체라서 앞서본 리스트와 같이 __iter__함수와 __next__함수를 사용할 수 있고 파이썬 기본함수인 iter, next 또한 사용할 수 있다. 다음의 간단한 키를 출력하는 딕셔너리에 대한 for 문을 while문으로 구현해 보자.

D = {'a':1, 'b':2, 'c':3}
for key in D.keys():
    print(key)
a
b
c
----------------------------------------
myiter = iter(D)
while True:
  try:
    print(next(myiter))
  except StopIteration:
    break
a
b
c

마무리하며

해당 내용을 공부하면서 iter객체의 존재의 이유에 대해 생각해보게 되었다. 정답이 아닐수도 있지만 iter객체를 사용하면 일반 array 자료구조에서 linked list 자료구조로 변환해 주는 것 같다.

linked list는 Tree라는 자료구조의 근간이 되는 자료구조이다.

profile
Work hard, Play hard

0개의 댓글