What does KeyError: 'text/plain' mean?

What does KeyError: 'text/plain' mean?

我在阅读 Guido 的博客“Tail Recursion Elimination”后玩游戏时遇到了这个错误。

我很好奇嵌套字典是否存在非递归限制,我确实收到了一条错误消息,但只是在非常特殊的情况下使用 IPython 控制台。

如果我降低 xrange(100 个有效),则没有错误。这里发生了什么?

thing = {}
thing2 = thing
for x in xrange(500):
    thing2[x] = {}
    thing2 = thing2[x]

thing

输出:

Traceback (most recent call last):

  File "<ipython-input-83-0b6d347b01d4>", line 1, in <module>
    thing

  File "C:\Python27\lib\site-packages\IPython\core\displayhook.py", line 255, in __call__
    self.log_output(format_dict)

  File "C:\Python27\lib\site-packages\IPython\core\displayhook.py", line 227, in log_output
    format_dict['text/plain']

KeyError: 'text/plain'


repr(thing)
Out[84]: '{0: {1: {2: {3: {4: ....{497: {498: {499: {}}}}}}}}'

这绝对是一个 IPython 问题。如果您尝试显示 "thing",它会失败(在 Python3 / IPython 4 中显示另一条错误消息)。但是,它是完全有效的对象。

这个(500万关卡)还可以(虽然创建需要几秒):

thing = {}
thing2 = thing
for x in range(5000000):
    thing2[x] = {}
    thing2 = thing2[x]
thing;