Why does the interpreter freeze when evaluating an expression?

Here is my experiment:

$ 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)

I understand that the interpreter is forever fixated on the “while True:” part, but why is it stuck in the evaluation a?

+4
source share
1 answer

anow a really large number, and it takes some time to print. Print it ain a loop and you will see that it becomes really big, this is just part of how big this amount is if you omit printing because printing takes time to complete. In addition, a note a=1always returns quickly 1.

+7
source

Source: https://habr.com/ru/post/1544202/


All Articles