(-1)**1000

Peter Otten __peter__ at web.de
Wed Oct 22 05:13:54 EDT 2014


ast wrote:

> If i am writing (-1)**1000 on a python program, will the
> interpreter do (-1)*(-1)*...*(-1) or something clever ?
> 
> In fact i have (-1)**N with N an integer potentially big.
> 
> I do some tests that suggest that Python is clever

Let's see:

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def f():
...     return (-1)**1000
... 
>>> dis.dis(f)
  2           0 LOAD_CONST               4 (1)
              3 RETURN_VALUE

So yes, CPython replaces the expression (-1)**1000 with its value during 
compilation.




More information about the Python-list mailing list