Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the interpreter hang when evaluating the expression?

Tags:

python

Here's 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 looped forever at the "while True: " part, but why did it get stuck evaluating a?

like image 283
d33tah Avatar asked Mar 20 '23 08:03

d33tah


1 Answers

a is now a really large number and it takes a while to print. Print a in the loop and you'll see it gets really big, this is just a fraction of how large it is if you omit the print, because print takes time to execute. Also, note a=1 always quickly returns 1.

like image 138
Dair Avatar answered Apr 09 '23 13:04

Dair