Python Forum
Why I get RecursionError on very small amount of data?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why I get RecursionError on very small amount of data?
#1
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]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#2
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).
Reply
#3
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get amount of bytes in a file chesschaser 1 1,544 Aug-23-2021, 03:24 PM
Last Post: deanhystad
  Moving large amount of data between MySql and Sql Server using Python ste80adr 4 3,326 Apr-24-2020, 01:24 PM
Last Post: Jeff900
  SQL query with a variable amount of parameters Antares 10 6,746 Jul-08-2019, 02:24 PM
Last Post: Antares
  RecursionError: maximum recursion depth exceeded in comparison ? leoahum 11 12,876 Mar-18-2019, 01:53 PM
Last Post: leoahum
  Have an amount of time to perform and action CookieGamez2018 1 2,916 Dec-21-2018, 07:12 AM
Last Post: Gribouillis
  search and replace first amount of strings instances with one thing and a second amou chickflick91 7 5,492 Sep-26-2017, 05:13 PM
Last Post: chickflick91
  Amount of letters in a inputted string? CyanSupreme 3 3,582 May-10-2017, 09:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020