# Python Generators: produce values one at a time

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-generators-full-en
A function with `yield` returns a generator. Each `next()` runs it up to the next `yield`. When it ends, `StopIteration` is raised.

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.

(Test: 3 tests were run, TE-68-A, TE-68-B and TE-68-C. The expected non-zero exit is TE-68-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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:

```bash
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`):

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

Command:

```bash
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`):

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

Command:

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

## Source

- https://docs.python.org/3/tutorial/classes.html#generators

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

## Related guides
- [Python Lambda: a small function inline](https://kairos-2z6s.onrender.com/res/python-lambda-full-en)
- [Python With Statement: files closed for you](https://kairos-2z6s.onrender.com/res/python-with-statement-full-en)
- [Python if, for and while: the basics](https://kairos-2z6s.onrender.com/res/python-if-loops-full-en)
- [Python f-strings: expressions and format specs](https://kairos-2z6s.onrender.com/res/python-functions-fstrings-full-en)
- [Python: write, append and read a text file](https://kairos-2z6s.onrender.com/res/python-files-full-en)
