5

If I make a deeply nested list, like this:

arr = [1]
for i in range(1000):
    arr = [arr]

then

print(arr)

will work fine, but

str(arr)

fails miserably with maximum recursion depth exceeded. ("%s" % arr, and repr(arr) too.)

How could I get the string that print prints? And what is the underlying reason for the difference?

Farhan.K
  • 3,425
  • 2
  • 15
  • 26
vagoston
  • 171
  • 8

2 Answers2

3

You can increase the recursion limit. But this safeguard is there for a reason. Are you sure this is what you want to do?

import sys

sys.setrecursionlimit(2000)

arr = [1]
for i in range(1000):
    arr = [arr]

str(arr)
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I would not dare to use such a code. It's just pure curiosity. Somehow, print in python 2.7 manages to overcome the maximum recursion problem. I would like to understand how. Certainly not with increasing recursion limit. – vagoston Feb 13 '18 at 22:23
  • @vagoston, understood. I put the disclaimer there to discourage other users who might attempt the same. I can't help with an explanation on `print` discrepancy in Python 2.7. – jpp Feb 13 '18 at 22:25
1

Just to state (one of) the obvious :) (Clearly, we could add handling for more nested instances other than lists, as well.)

arr = [1]
for i in range(1000):
  arr = [arr]

def showNested(arr):
  stack = [arr]
  s = ''

  while stack:
    curr = stack.pop()

    if isinstance(curr, list):
      s = s + '['
      stack.append(']')

      for v in curr:
        stack.append(v)

    else:
      s = s + str(curr)

  return s

Output:

print(showNested(arr)) # Python 3.6.1   Where's Waldo?
=> [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • Well, now that you say, it's pretty much obvious. There remains only a rhetoric question. Why is there a difference in 2.7 print and repr. – vagoston Feb 14 '18 at 09:59
  • @vagoston Sorry, I forgot about showing commas :) Might need a little more work...maybe as exercise for the reader? – גלעד ברקן Feb 14 '18 at 10:26