본문 바로가기

Python/중급

[Python] 병행성(Concurrency) - basic

  • iterable 객체 - 반복 가능한 객체
  • iterator 객체 - 값을 차례대로 꺼낼 수 있는 객체

 

iterable 객체 

 

# 반복 가능한 이유? -> 내부적으로 iter() 함수 호출
t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# for 반복
for c in t:
    print(c, end=' ')
    
--------------------------------------------[result]

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

 

iterator 객체

 

a=t.__iter__()
print([next(a) for _ in range(len(t))])

# while 반복
w = iter(t)

while True:
    try:
        print(next(w), end=' | ')
    except StopIteration:
        break

--------------------------------------------[result]

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 

 

반복적 객체 확인

 

from collections import abc

# 반복형 확인
print(hasattr(t, '__iter__'))
print(isinstance(t, abc.Iterable))
print(type(t))

--------------------------------------------[result]

True
True
< class 'str' >

 

Generator 미사용 샘플 코드

 

# next 사용
class WordSplitIter:
    def __init__(self, text):
        self._idx = 0
        self._text = text.split(' ')
    
    def __next__(self):
#         print('Called __next__')
        try:
            word = self._text[self._idx]
        except IndexError:
            raise StopIteration('Stopped Iteration.^^')
        self._idx += 1
        return word

    def __repr__(self):
        return 'WordSplit(%s)' % (self._text)

wi = WordSplitIter('Do today what you could do tomorrow')

print(wi)
print(next(wi))
print(next(wi))
print(next(wi))
print(next(wi))
print(next(wi))
print(next(wi))
print(next(wi))
# print(next(wi))

--------------------------------------------[result]

WordSplit(['Do', 'today', 'what', 'you', 'could', 'do', 'tomorrow'])
Do
today
what
you
could
do
tomorrow

 

Generator 사용 샘플 코드

 

# Comprehension 패턴 (https://wikidocs.net/22805)
# Generator 패턴 (https://wikidocs.net/16069)
# 1.지능형 리스트, 딕셔너리, 집합 -> 데이터 양 증가 후 메모리 사용량 증가 -> 제네레이터 사용 권장
# 2.단위 실행 가능한 코루틴(Coroutine) 구현과 연동
# 3.작은 메모리 조각 사용

class WordSplitGenerator:
    def __init__(self, text):
        self._text = text.split(' ')
    
    def __iter__(self):
        # print('Called __iter__')
        for word in self._text:
           yield word # 제네레이터
        return
    
    def __repr__(self):
        return 'WordSplit(%s)' % (self._text)

wg = WordSplitGenerator('Do today what you could do tomorrow')

wt = iter(wg)

print('wg', wg)
print('wt', wt)
print(next(wt))
print(next(wt))
print(next(wt))
print(next(wt))
print(next(wt))
print(next(wt))
print(next(wt))
# print(next(wt))

--------------------------------------------[result]

wg WordSplit(['Do', 'today', 'what', 'you', 'could', 'do', 'tomorrow'])
wt <generator object WordSplitGenerator.__iter__ at 0x000001F4295DCD58>
Do
today
what
you
could
do
tomorrow

'Python > 중급' 카테고리의 다른 글

[Python] 병행성(Concurrency) - Coroutine  (0) 2021.05.26
[Python] 병행성(Concurrency) - generator  (0) 2021.05.25
[Python] Decorator  (0) 2021.05.25
[Python] Closure  (0) 2021.05.25
[Python] 일급함수  (0) 2021.05.24