简体   繁体   中英

Python: Weird behavior while using `yield from`

In the following code, I have run into a RecursionError: maximum recursion depth exceeded .

def unpack(given):
    for i in given:
        if hasattr(i, '__iter__'):
            yield from unpack(i)
        else:
            yield i

some_list = ['a', ['b', 'c'], 'd']

unpacked = list(unpack(some_list))

This works fine if I use some_list = [1, [2, [3]]] , but not when I try it with strings.

I suspect my lack of knowledge in python. Any guidance appreciated.

Strings are infinitely iterable. Even a one-character string is iterable.

Therefore, you will always get a stack overflow unless you add special handling for strings:

def flatten(x):
    try:
        it = iter(x)
    except TypeError:
        yield x
        return
    if isinstance(x, (str,  bytes)):
        yield x
        return
    for elem in it:
        yield from flatten(elem)

Note: using hasattr(i, '__iter__') is not sufficient to check if i is iterable, because there are other ways to satisfy the iterator protocol. The only reliable way to determine whether an object is iterable is to call iter(obj) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM