为什么解释器在评估表达式时会挂起?

d33*_*tah 4 python

这是我的实验:

$ python
Python 2.7.5 (default, Feb 19 2014, 13:47:28) 
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 3
>>> while True:
...   a = a * a
... 
^CTraceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>> a
(seems to go on forever)
Run Code Online (Sandbox Code Playgroud)

据我所知,解释器永远在"while True:"部分循环,但为什么它会被卡住评估a

Dai*_*air 7

a现在是一个非常大的数字,打印需要一段时间.a在循环中打印,你会看到它变得非常大,这只是省略打印的大小的一小部分,因为打印需要时间来执行.此外,注意a=1总是快速返回1.

  • 更具体一点,它不是*打印*慢,它是整数转换为字符串. (2认同)