Python Tutorials - Herong's Tutorial Examples - v2.15, by Herong Yang
What Is Generator Iterator
This section provides a quick introduction of generator iterator, which is created by a generator function or a generator expression.
What Is Generator Iterator - A generator iterator is an iterator object created by a generator function or a generator expression.
A generator iterator is also iterable. iter(generator) returns the generator itself.
What Is Generator Function - A generator function is a special function that contains one or more "yield" statements.
What Is "yield" Statement - A "yield" statement is a simple statement with this syntax:
yield EXPRESSION
The logic of using a generator function can be described as:
In order to understand the behavior of "yield" statements in a generator function, I wrote the following tutorial example, yield_test.py:
# yield_test.py #- Copyright 2022 (c) HerongYang.com. All Rights Reserved. # def get_squares(n): i = 0 while i < n: print('get_squares():', i) yield i*i print('get_squares(): after yield') i += 1 g = get_squares(4) print("object g: id and type =", id(g), type(g)) i = iter(g) print("object i: id and type =", id(i), type(i)) print(next(i)) print(next(i)) print(next(i)) print(next(i)) print(next(i)) print(next(i))
If you run this sample code, you should get:
herong$ python yield_test.py ('object g: id and type =', 4528495104, <type 'generator'>) ('object i: id and type =', 4528495104, <type 'generator'>) ('get_squares():', 0) 0 get_squares(): after yield ('get_squares():', 1) 1 get_squares(): after yield ('get_squares():', 2) 4 get_squares(): after yield ('get_squares():', 3) 9 get_squares(): after yield Traceback (most recent call last): File "yield_test.py", line 23, in <module> print(next(i)) StopIteration
The output confirms that Python system converts the get_squares() function into a "get_squares" class, and implements both __iter()__() and __next__() methods. So the function call get_squares(5) becomes a constructor call to create a "generator" object, which is an "iterable" and "iterator" at the same time.
Of course, you can use get_squares(5) in a "for" loop directly.
for x in get_squares(5): print(x)
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
Iterable Objects of Built-in Data Types
What Is Filtered Generator Expression
What Is Double-Generator Expression
List, Set and Dictionary Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager