Python Forum
Why I get RecursionError on very small amount of data? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why I get RecursionError on very small amount of data? (/thread-12014.html)



Why I get RecursionError on very small amount of data? - wavic - Aug-05-2018

I can't imagine how is possible to get this error when I use the function on such a small data Huh
How is that even possible?
In [1]: l = [list(word) for word in 'Something goes wrong'.split()]

In [2]: def flatten(iterable):
   ...:     iterator = iter(iterable)
   ...:     for item in iterator:
   ...:         if '__iter__' in dir(item):
   ...:             yield from flatten(item)
   ...:         else:
   ...:             yield item
   ...:             

In [3]: flat = [item for item in flatten(l)]

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-3-930115f14cc4> in <module>()
----> 1 flat = [item for item in flatten(l)]

<ipython-input-3-930115f14cc4> in <listcomp>(.0)
----> 1 flat = [item for item in flatten(l)]

<ipython-input-2-7311442c70ce> in flatten(iterable)
      3     for item in iterator:
      4         if '__iter__' in dir(item):
----> 5             yield from flatten(item)
      6         else:
      7             yield item

... last 1 frames repeated, from the frame below ...

<ipython-input-2-7311442c70ce> in flatten(iterable)
      3     for item in iterator:
      4         if '__iter__' in dir(item):
----> 5             yield from flatten(item)
      6         else:
      7             yield item

RecursionError: maximum recursion depth exceeded while calling a Python object

In [4]: 

In [4]: flat = [item for item in flatten(l)]
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-4-930115f14cc4> in <module>()
----> 1 flat = [item for item in flatten(l)]

<ipython-input-4-930115f14cc4> in <listcomp>(.0)
----> 1 flat = [item for item in flatten(l)]

<ipython-input-2-7311442c70ce> in flatten(iterable)
      3     for item in iterator:
      4         if '__iter__' in dir(item):
----> 5             yield from flatten(item)
      6         else:
      7             yield item

... last 1 frames repeated, from the frame below ...

<ipython-input-2-7311442c70ce> in flatten(iterable)
      3     for item in iterator:
      4         if '__iter__' in dir(item):
----> 5             yield from flatten(item)
      6         else:
      7             yield item

RecursionError: maximum recursion depth exceeded while calling a Python object

In [5]: 



RE: Why I get RecursionError on very small amount of data? - micseydel - Aug-05-2018

Without having dug into it, I would imagine it's because your base case is characters, which are length-1 strings, which are iterable. I expect it would work if the smallest elements are scalars (e.g int).


RE: Why I get RecursionError on very small amount of data? - wavic - Aug-05-2018

Hm! I have tried it with integers and it worked. It's strange to me.
sys.getrecursionlimit() says that it is 1000.
How it happens if an element is a string ( iterable) this limit is reached? This is not happening during code execution I suppose.


RE: Why I get RecursionError on very small amount of data? - micseydel - Aug-05-2018

(Aug-05-2018, 04:48 PM)wavic Wrote: How it happens if an element is a string ( iterable) this limit is reached?
Output:
>>> '__iter__' in dir('c') True