Question about math.pi is mutable

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 11 02:38:30 EST 2015


On Wednesday 11 November 2015 00:26, BartC wrote:

> Does the Python language specify how it is to be compiled and executed?

Not in so many words, but there are limitations on what you can do based on 
the specified semantics of Python.

But so long as you meet those semantics, you can implement them any 
reasonable way you like:

- CPython uses one specific implementation written in C;

- PyPy uses a powerful tracing JIT compiler written in an intermediate 
language RPython;

- IronPython uses the .Net CLR virtual machine;

- Jython uses the Java virtual machine;

- there was an experimental version of Python using the Parrot virtual 
machine;

etc. Those implementations will naturally perform differently. For example, 
I believe that IronPython *generally* is faster than CPython, with some 
exceptions.


> If not, then you can use any interpretation you like, provided the
> program gives the expected results.

Precisely.

> That includes using a static compilation pass that generates byte-code,
> even if it is only done immediately before running the main module, or
> just before performing an import operation on another.

That's what CPython already does.


> Anything is possible. But the chances are that if you are running
> CPython, then it will probably include a discrete byte-code compiler.

Since compile, eval and exec are Python built-ins, if it doesn't include a 
byte-code compiler, it isn't Python. It's just a subset of Python.



>> execute this Python snippet:
>>
>> result = x + 2
[...]
>> But a JIT compiler gets to compile code right before that line is due to
>> execute. By the time the JIT compiler gets to see that line of code, it
>> can already know whether or not name "x" exists, and if so, which
>> namespace it is in. It knows what value "x" has.
> 
> How does it do that? How is the 'x+1' even stored in the machine?

That depends entirely on the implementation of the compiler.


> Suppose the preceding lines are:
> 
>     if random+bit()==1:
>        x=920.5
>     else:
>        x=[8,5,4,9,1,7,6,3,2]
> 
> or:
> 
>     if some_condition: del x
> 
> it will probably know as much about x as the static compiler does!

Not at all. The difference is, having called rand_bit() and tested whether 
it is 1, the compiler takes a branch:

- it binds 920.5 to x

- or it binds [8, 5, 4, ...] to x

The compiler knows which branch it just took, *because it just took it*. If 
it doesn't know which branch it just took, it isn't a JIT compiler! It's 
just a classic, old-fashioned interpreter with no smarts.

So, having just bound some value to x, the compiler knows that x is a float 
(or a list), and can optimize the next instruction, which is "x + 1".

CPython can't do that, because it doesn't have the infrastructure to perform 
the necessary book-keeping. And it probably never will do that, because 
Guido likes the fact that CPython is simple enough for him to understand. 
He's happy for PyPy and other third-party implementations to do the clever 
stuff.



-- 
Steven




More information about the Python-list mailing list