KKAIROS
Practical guideIllustrative3 min read

Python Generators: produce values one at a time

A function with yield returns a generator. Each next() runs it up to the next yield. When it ends, StopIteration is raised.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-68-A: a generator yields lazily and is exhausted after one pass

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def count(n):
    for i in range(n):
        yield i
g = count(3)
print(next(g))
print(list(g))
print(list(g))
try:
    next(g)
except StopIteration:
    print('done')
EOS

Command:

python3 s.py

Recorded output:

0
[1, 2]
[]
done

Exit status: 0. The output matches the expected text written before the run.

TE-68-B: sum and zip can consume a generator, and each call to the function makes a fresh generator

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def g():
    yield 1
    yield 2
print(sum(g()))
print(list(zip(g(), 'ab')))
EOS

Command:

python3 s.py

Recorded output:

3
[(1, 'a'), (2, 'b')]

Exit status: 0. This status and the output match what was written before the run.

TE-68-C: expected non-zero exit: calling next on an exhausted generator raises StopIteration when nothing catches it

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def g():
    yield 1
x = g()
next(x)
next(x)
EOS

Command:

python3 s.py

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "/tmp/b17-2_s15ghr/work/s.py", line 5, in <module>
    next(x)
StopIteration

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-68-B: sum of one generator printed 3. A second call to g() made a new generator, and zip with 'ab' paired it as [(1, 'a'), (2, 'b')]. TE-68-C: the second next() on a generator with one yield was not caught. The program ended with exit status 1 and standard error ends with StopIteration.

Limits

Generator expressions and send() were not tested. A generator can be consumed only once.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-68-A | a generator yields lazily and is exhausted after one pass | 0 | PASS | 2cd1be99adcf | | TE-68-B | sum and zip can consume a generator, and each call to the function makes a fresh generator | 0 | PASS | d8d9cf5b0c8c | | TE-68-C | expected non-zero exit: calling next on an exhausted generator raises StopIteration when nothing catches it | 1 | PASS | 6de43382e0ea |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides