상세 컨텐츠

본문 제목

iterable, iterator, generator, yield

Python

by techbard 2024. 12. 22. 19:02

본문

반응형

http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained

 

Iterables

To understand what yield does, you must understand what generators are. And before generators come iterables. When you create a list, you can read its items one by one, and it's called iteration :

>>> mylist = [1, 2, 3]
>>> for i in mylist :
...    print(i)
1
2
3

Mylist is an iterable. When you use a comprehension list, you create a list, and so an iterable :

>>> mylist = [x*x for x in range(3)]
>>> for i in mylist :
...    print(i)
0
1
4

Everything you can use "for... in..." on is an iterable : lists, strings, files... These iterables are handy because you can read them as much as you wish, but you store all the values in memory and it's not always what you want when you have a lot of values.

Generators

Generators are iterables, but you can only read them once. It's because they do not store all the values in memory, they generate the values on the fly :

>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator :
...    print(i)
0
1
4

It just the same except you used () instead of []. BUT, you can not perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1 and ends calculating 4, one by one.

Yield

Yield is a keyword that is used like return, except the function will return a generator.

>>> def createGenerator() :
...    mylist = range(3)
...    for i in mylist :
...        yield i*i
...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object !
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)
0
1
4

Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.

To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is bit tricky :-)

Then, your code will be run each time the for uses the generator.

Now the hard part :

The first time your function will run, it will run from the beginning until it hits yield, then it'll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return.

The generator is considered empty once the function runs but does not hit yield anymore. It can be because the loop had come to ends, or because you do not satisfy a "if/else" anymore.

Your code explained

Generator:

# Here you create the method of the node object that will return the generator
def node._get_child_candidates(self, distance, min_dist, max_dist):

  # Here is the code that will be called each time you use the generator object :

  # If there is still a child of the node object on its left
  # AND if distance is ok, return the next child
  if self._leftchild and distance - max_dist < self._median:
                yield self._leftchild

  # If there is still a child of the node object on its right
  # AND if distance is ok, return the next child
  if self._rightchild and distance + max_dist >= self._median:
                yield self._rightchild

  # If the function arrives here, the generator will be considered empty
  # there is no more than two values : the left and the right children

Caller:

# Create an empty list and a list with the current object reference
result, candidates = list(), [self]

# Loop on candidates (they contain only one element at the beginning) 
while candidates:

    # Get the last candidate and remove it from the list
    node = candidates.pop()

    # Get the distance between obj and the candidate
    distance = node._get_dist(obj)

    # If distance is ok, then you can fill the result
    if distance <= max_dist and distance >= min_dist:
        result.extend(node._values)

    # Add the children of the candidate in the candidates list 
    # so the loop will keep running until it will have looked
    # at all the children of the children of the children, etc. of the candidate
    candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))

return result

This code contains several smart parts :

  • The loop iterates on a list but the list expands while the loop is being iterated :-) It's a concise way to go through all these nested data even if it's a bit dangerous since you can end up with an infinite loop. In this case, candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) exhausts all the values of the generator, but while keep creating new generators object which will produce different values from the previous ones since it's not applied on the same node.
  • The extend() method is a list object method that expects an iterable and adds its values to the list.

Usually we pass a list to it :

>>> a = [1, 2]
>>> b = [3, 4]
>>> a.extend(b)
>>> print(a)
[1, 2, 3, 4]

But in your code it gets a generator, which is good because :

  1. You don't need to read the values twice.
  2. You can have a lot of children and you don't want them all stored in memory.

And it works because Python does not care if the argument of a method is a list or not. Python expects iterables so it will work with strings, lists, tuples and generators ! This is called duck typing and is one of the reason why Python is so cool. But this is another story, for another question...

You can stop here, or read a little bit to see a advanced use of generator :

Controlling a generator exhaustion

>>> class Bank(): # let's create a bank, building ATMs
...    crisis = False
...    def create_atm(self) :
...        while not self.crisis :
...            yield "$100"
>>> hsbc = Bank() # when everything is fine, you can get as much money as you want from an ATM
>>> corner_street_atm = hsbc.create_atm()
>>> print(corner_street_atm.next())
$100
>>> print(corner_street_atm.next())
$100
>>> print([corner_street_atm.next() for cash in range(5)])
['$100', '$100', '$100', '$100', '$100']
>>> hsbc.crisis = True # crisis is coming, no more money !
>>> print(corner_street_atm.next())
<type 'exceptions.StopIteration'>
>>> wall_street_atm = hsbc.create_atm() # but it's true even for newly built ATM
>>> print(wall_street_atm.next())
<type 'exceptions.StopIteration'>
>>> hsbc.crisis = False # trouble is, when the crisis is off, the ATM are still empty...
>>> print(corner_street_atm.next())
<type 'exceptions.StopIteration'>
>>> brand_new_atm = hsbc.create_atm() # but if you build new ones, you're in business again !
>>> for cash in brand_new_atm :
...    print cash
$100
$100
$100
$100
$100
$100
$100
$100
$100
...

It can be useful for various things like controlling access to a resource.

Itertools, your best friend

The itertools module contains special functions to manipulate iterables. Ever wish to duplicate a generator ? Chain a two generators ? Groups values in nested list with a one liner ? Map / Zip without creating another list ?

Then just import itertools.

An example ? Let's see the possible orders of arrival for a 4 horses race:

>>> horses = [1, 2, 3, 4]
>>> races = itertools.permutations(horses)
>>> print(races)
<itertools.permutations object at 0xb754f1dc>
>>> print(list(itertools.permutations(horses)))
[(1, 2, 3, 4),
 (1, 2, 4, 3),
 (1, 3, 2, 4),
 (1, 3, 4, 2),
 (1, 4, 2, 3),
 (1, 4, 3, 2),
 (2, 1, 3, 4),
 (2, 1, 4, 3),
 (2, 3, 1, 4),
 (2, 3, 4, 1),
 (2, 4, 1, 3),
 (2, 4, 3, 1),
 (3, 1, 2, 4),
 (3, 1, 4, 2),
 (3, 2, 1, 4),
 (3, 2, 4, 1),
 (3, 4, 1, 2),
 (3, 4, 2, 1),
 (4, 1, 2, 3),
 (4, 1, 3, 2),
 (4, 2, 1, 3),
 (4, 2, 3, 1),
 (4, 3, 1, 2),
 (4, 3, 2, 1)]

Understanding the inner mechanisms of iteration

Iteration is a process implying iterables (implementing the __iter__() method) and iterators (implementing the __next__() method). Iterables are any objects you can get an iterator from. Iterators are objects that let you iterate on iterables.

More about it in in this article about how does the for loop work.

Oh, and if you liked this answer, you'll probably like my explanation for decorators and metaclasses.

 

import random
from typing import Generator

symbol: str = 'x'
with open('c://windows//temp//temp_file.txt', 'w') as f:
    for _ in range(10):
        f.write(symbol * random.randint(0, 100))
        f.write('\n')

# list comprehension 버전
# value: list[int] = [len(a) for a in open('c://windows//temp//temp_file.txt')]
# print(value)

# 대량의 컴프리헨션의 생성이 부담되는 경우, 대신에 generator expression을 사용하면
it: Generator[int, None, None] = (len(a) for a in open('c://windows//temp//temp_file.txt'))
print(it)

roots: tuple[int, float] = ((x, x**0.5)for x in it)
print(next(roots))
print(next(roots))
print(next(roots))
print(next(roots))

# 결과
<generator object <genexpr> at 0x0000025B24E929B0>
(86, 9.273618495495704)
(64, 8.0)
(11, 3.3166247903554)
(11, 3.3166247903554)

 

# Generator Expression
nums: list[int] = [n for n in range(1, 100)]
even_gen = (n for n in nums if n % 2 == 0)
print(f"gen obj: {even_gen}")
print(f"sum: {sum(even_gen)}")

# Generators can only be consumed once!
even_gen = (n for n in nums if n % 2 == 0)
print(f"len: {sum(1 for _ in even_gen)}")

# 결과
gen obj: <generator object <genexpr> at 0x00000214F72A31D0>
sum: 2450
len: 49

 

# log 파일에서 에러 라인을 찾아 개수 카운트 하기
# generator를 사용해서 메모리 사용 최소화

with open('server.log') as f:
    error_lines = (1 for line in f if "ERROR" in line)

error_count = sum(error_lines)

 

# If a user did need the list, they have to transform the generator to a list
# with list(range(0, 10))
# 이렇게 하는 경우 메모리에 매우 큰 리스트를 저장할 필요가 없어 효율적이다.

# 제너레이터를 만들어 보자.

# 일반 함수
def create_cubes(end_num: int) -> list[int]:
    res: list[int] = []
    for curr_num in range(end_num):
        res.append(curr_num**3)
    return res

cubes: list[int] = create_cubes(10)
print(f"{cubes    = }") # 이렇게 되면 사실 메모리에 모든 리스트를 저장한다. 메모리에 계속 유지하지 말고 yield 사용.

# 제너레이터로 구현
from typing import Generator

def create_cubes_gr(nums: int) -> Generator[int] :
    for n in range(nums):
        yield n**3

cubes_gr: list[int] = list(create_cubes_gr(10))
print(f"{cubes_gr = }")

# 결과
cubes    = [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
cubes_gr = [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

 

from typing import Generator

def simple_gen() -> Generator[int]:
    for n in range(3):
        yield n

for num in simple_gen():
    print(f"{num = }", end=' ')

print("")
gen_obj = simple_gen()
print(gen_obj)
print(next(gen_obj), end=' ')
print(next(gen_obj), end=' ')
print(next(gen_obj))
# for - loop에서는 StopIteration 에러가 발생할 때까지 next() 호출한다.

# 결과
num = 0 num = 1 num = 2 
<generator object simple_gen at 0x000002B85F2F3B80>
0 1 2

 

# iter(callable, sentinel)
# callable: 호출이 가능한 객체
# sentinel: 반복하다가 특정 값이 나오면 반복을 중단 / 반복을 감시하다가 특정 값이 나오면 반복을 끝내는 sentinel

# 문자열을 이터레이터로 변환
# text: str = 'hello world!'

# it = iter(text)
# for _ in range(len(text)):
#     print(next(it), end=' ')

# 호출 객체를 부르고, 중단점 설정
import random
from typing import Iterator

random_int_it: Iterator[int] = iter(lambda :random.randint(1, 10), 10)
# 10이 나올 때까지 반복

for i in random_int_it:
    print(i, end=' ')

# 결과
4 7 5 5

 

# random number generator 구현하기
import random

def create_1to10(n=10):
    while True:
        yield random.randrange(n)

def samples(limit, generator):
    for i, n in enumerate(generator, start=1):
        if i == limit: break
        yield n

random.seed(1)
result = list(samples(10, create_1to10()))
print(result)

# 결과
[2, 9, 1, 4, 1, 7, 7, 7, 6]

 

# 계수기 클래스, 랜덤 정수 제너레이터 사용
from collections import Counter
import random

def random_num_generator(n=10):
    while True:
        yield random.randrange(n)

random_ints = [next(random_num_generator()) for _ in range(100)]
d = Counter(random_ints)
print(d)

print(f"100 numbers created? {sum([v for v in d.values()])}")

# 결과
Counter({2: 14, 5: 12, 3: 12, 1: 10, 7: 10, 6: 10, 0: 10, 9: 10, 4: 7, 8: 5})
100 numbers created? 100

 

# Iterators
# =========
# Iteratoris an object that allows you to iterate over collections of data,
# such as lists, tuples, dictionaries, and sets.
# It implements the Iterator design pattern, which allows you to
# traverse a contatiner and access its elements.

# Iterable
ns = [1, 2, 3, 4, 5]

# Iterator
iter = ns.__iter__()

print(next(iter))
print(next(iter))
print(next(iter))

# Output:
1
2
3

 

class CountUp:
    def __init__(self, start, end):
        self.current = start
        self.end = end
    
    def __iter__(self):
        return self

    def __next__(self):
        if self.current > self.end:
            raise StopIteration
        current = self.current
        self.current += 1
        return current

counter = CountUp(1, 5)

for number in counter:
    print(number, end=' ')

# Output:
1 2 3 4 5

 

# Generators
# ==========
# Standard Functions: Compute a value and reutrn it once using the return statement.
# Generators: Use the yield statement to return values one at a time withot
# storing all the values in memory.

def generate_numbers():
    # return [1, 2, 3]
    for i in range(1, 4):
        yield i

for num in generate_numbers():
    print(num)

list_comp =  [i for i in range(10)]
print(list_comp)

list_gen = (i for i in range(10))
print(list_gen)
print(next(list_gen))
print(next(list_gen))

# ==============================

def count_up(start, end):
    while start <= end:
        yield start
        start += 1

# Create a generator
gen = count_up(1, 3)

# Iterate through the generator
for number in gen:
    print(number, end=' ')

# Generators Use Cases
# ====================
# Working with Large Datasets: Loading large files in meemory
# can freeze your program or cause a MemoryError. Generators can fix this.
# Better Performance: Generators avoid eagerly computing
# all data and storing them in memory. They're only computed when needed,
# improving the overall performance.
# Infinite Sequences: Generators can geerate an infinite sequence which
# can be useful for testing.

# Output:
1
2
3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x000001FED83F5B40>
0
1
1 2 3
반응형

관련글 더보기

댓글 영역