[Python] Generator function

Introduction

This article is an article for studying generator functions because it failed to be used without understanding the generator functions. The content is a personal memorandum with references.

Definition

First, check the definition of the generator function.

--Definition: Generator function [^ 1]

When a yield statement is used in a function definition, the function is called a generator function. Generator functions are a type of iterator.

Example: Execute a generator function using yield in Python 3.7.4. Returns values separated by yield.

def sample_generator_fun():
    yield 1
    yield 2
    yield 3

check = sample_generator_fun()
print(check.__next__())
print(check.__next__())
print(check.__next__())

Execution result

1
2
3

If you run another check ...

print(check.__next__())
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-25-7029da5797f2> in <module>
----> 1 print(check.__next__())

StopIteration:

It will be displayed as stop repeating and an error will occur. That is, the yield statements of sample_generator_fun will be executed in sequence, the iteration will end when the third "yield 3" is executed, and no value will be returned even if it is executed further.

Failure example

I didn't understand the generator function properly, thought it as the same as an iterator object such as a list, and made the following mistakes. * In the first place, I was ashamed to not know the existence of the generator function.

Example: Check if the number of iterators of the obtained generator function is more than 2, and if so, execute it.

** Failure example **

check = sample_generator_fun()

if len(list(check)) > 2:
    print("True")
    for i in check:
        print(i)
else:
    print("False")

Execution result

True

Here, I was wondering why the for statement below it was not executed even though it was True in the if statement, and I was able to find out various things and arrive at the generator function. List (check) in the conditional expression By executing, the last "yield 3" is executed, so the for statement is not executed because check does not return a value in the next for statement.

solution

There are two things that you can easily think of: [This article] If you want to make the generator function callable multiple times. (https://qiita.com/tomotaka_ito/items/15b5999c76001dbc9a58) It may be better to do something like [^ 2].

--If the total data size returned by the generator function is not large, convert it to a list and use it.

check = sample_generator_fun()
check_list = list(check)

if len(check_list) > 2:
    print("True")
    for i in check_list:
        print(i)
else:
    print("False")

Execution result

True
1
2
3

--Simply call the generator function twice. In this case, the memory savings that are the advantage of the generator function are preserved.

check = sample_generator_fun()

n = 0
for i in check:
    n += 1
    if n > 2:
        print("True")
        check = sample_generator_fun()
        for j in check:
            print(j)
        break
else:
    print("False")

Execution result

True
1
2
3

Impressions

In scripting languages, you can write code without declaring types, but if you don't know exactly what type and nature you are touching, you will get an accident. This time, it's the same as "list". It was a failure due to wishful thinking. It is difficult to grasp all the contents of the library to be used, but I realized once again that the basic things like this time should be suppressed properly.

[^ 1]: Kenji Nakaku: Introduction to Python for Science and Technology Calculations, 2016, Gijutsu-Hyoronsha [^ 2]: I want to iterate the Python generator many times

Recommended Posts

[Python] Generator function
python function ①
python function ②
python enumerate function
Python> function> Closure
Python> function> Inner function
Python function decorator
About function arguments (python)
Python function argument summary
Python print function (sequel)
Python: About function arguments
Time floor function (Python)
Zundokokiyoshi (generator) in Python
Create a function in Python
Python
[python] Value of function object (?)
2017-04-11 Python> I tried generator> I was taught generator expression / generator function / next ()
ntile (decile) function in python
[Python] Etymology of python function names
generator
About the enumerate function (python)
python url setting include function
Python #function 2 for super beginners
Nonlinear function modeling in Python
Draw implicit function in python
Python higher-order function (decorator) sample
Immediate function in python (lie)
Generator
[Python] What is a zip function?
Python tuple comprehensions and generator expressions
Call a Python function from p5.js.
[python] Callback function (pass function as argument)
Function argument type definition in python
random French number generator with python
Infinite prime number generator in Python3
Measure function execution time in Python
[Introduction to Udemy Python3 + Application] 59. Generator
[Python] Make the function a lambda function
Python #len function for super beginners
Function synthesis and application in Python
Precautions when creating a Python generator
Basic Python operation 2nd: Function (argument)
How to use python zip function
[Python] Difference between function and method
[Python] Function arguments * (star) and ** (double star)
[OpenCV; Python] Summary of findcontours function
kafka python
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
Python3> Functions> Function Renaming Mechanism> f = fib
Python basics ⑤
python + lottery 6
Create a Python function decorator with Class
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
Realize PHP / Python generator with Golang / Ruby