python-cookbook icon indicating copy to clipboard operation
python-cookbook copied to clipboard

Missing ignore_types arg when calling the method of flatten recursively

Open xiaofeig opened this issue 5 years ago • 0 comments

The code has missed ignore_types arg when calling the method of flatten recursively in the example about how to flatten a nested sequence.

src/4/how_to_flatten_a_nested_sequence/example.py

from collections import Iterable

def flatten(items, ignore_types=(str, bytes)):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, ignore_types):
            yield from flatten(x)
        else:
            yield x

The corrected code is yield from flatten(x, ignore_types).

xiaofeig avatar Mar 21 '19 09:03 xiaofeig