为什么在大型数组的计算中系统只是挂起而不引发内存错误?

Why in the calculation of large arrays the system simply hangs and doesn't raise a memory error?

我的演示文稿需要内存不足系统错误。我只想通过计算得到它,而不是试图打开一个大文件。 我尝试使用理解来计算一个大列表:

ll = [x**x**x**x**x for x in range(100000)]

但是系统只是挂了,没有给出任何错误。

任何人都可以就如何执行此操作提供一些建议吗?

您在 中写道:

Method matters. I would like to use this example to show the advantage of generators over ordinary lists.

我不确定使用数字无法完成记忆的确切原因,但对于您的实验:

ll = ['just a sample string' * x for x in range(100000)]

确实会提高 MemoryError,同时:

gen = ('just a sample string' * x for x in range(100000))
for _ in range(5):
    print(next(gen))

将打印:


just a sample string
just a sample stringjust a sample string
just a sample stringjust a sample stringjust a sample string
just a sample stringjust a sample stringjust a sample stringjust a sample string

甚至相当快...