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.
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.
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.
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
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