About python yield

Generator using yield

Python has a command called yield to make it easy to create a generator. I didn't really understand the behavior, so I'll leave a note below.

basic action

test.py


def yield_test():
    list0to4 = [0, 1, 2, 3, 4]
    for i in list0to4:
        yield i

for j in yield_test()
    print j

If you run the above code, the output will be as follows.

0
1
2
3
4

All the elements of [0, 1, 2, 3, 4] can be read in order.

Combine generators

Now consider the case of using another generator in combination.

test.py


def yield_test1():
    list0to4 = [0, 1, 2, 3, 4]
    for i in list0to4:
        yield i

def yield_test2():
    list0to2 = [0, 1, 2]
    for i in list0to2:
        yield i

iter0to2 = yield_test2()
for j in yield_test1()
    print j + iter0to2.next()

In this case, the output will be as follows.

0
2
4

yield_test2 has fewer elements than yield_test1, but there are no particular errors.

Generator operation

The generator raises an exception [StopIteration] when it goes to read the next without the next element. In the for statement etc., it seems that the loop is stopped by excluding this [Stop Iteration]. Therefore, in the above example, iter0to2.next () raises [StopIteration]. Even if the element remains on the yield_test1 () side, the for statement will be omitted.

When using a combination of generators, which one caused the loop to exit? Please note that it may be difficult to understand.

Recommended Posts

About python yield
About python slices
About python comprehension
About Python tqdm.
About python, class
About python inheritance
About python, range ()
About python decorators
About python reference
About Python decorators
[Python] About multi-process
About Python for loops
Summary about Python scraping
About function arguments (python)
[Python] Memo about functions
Summary about Python3 + OpenCV3
About Python, for ~ (range)
About Python3 character code
[Python] Memo about errors
About Python development environment
Python: About function arguments
Python, about exception handling
About Python Pyramid traversal
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
Flatten using Python yield from
[Python] Find out about pip
About Fabric's support for Python 3
Python
About python objects and classes
About Python variables and objects
About the Python module venv
Think about architecture in python
About python beginner's memorandum function
About the ease of Python
About the enumerate function (python)
About various encodings of Python 3
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
About Python datetime and timezone
A memorandum about correlation [Python]
A memorandum about Python mock
About Python string comparison operators
About Python and regular expressions
About the features of Python
About "for _ in range ():" in python
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
A note about [python] __debug__
Python, yield, return, and sometimes yield from
Python: A Note About Classes 1 "Abstract"
[Python] Let's write briefly about comprehensions
About python dict and sorted functions
About dtypes in Python and Cython
[Python] What is @? (About the decorator)
What was surprising about Python classes
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes