Reputation: 354
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.
Upvotes: 2
Views: 193
Reputation: 362478
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)
.
Upvotes: 7