@@ -380,11 +380,24 @@ is exactly the same type of object that a lambda form yields) is assigned!
380380Can Python be compiled to machine code, C or some other language?
381381-----------------------------------------------------------------
382382
383- Not easily. Python's high level data types, dynamic typing of objects and
383+ Practical answer:
384+
385+ `Cython <http://cython.org/ >`_ and `Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ >`_
386+ compile a modified version of Python with optional annotations into C
387+ extensions. `Weave <http://www.scipy.org/Weave >`_ makes it easy to
388+ intermingle Python and C code in various ways to increase performance.
389+ `Nuitka <http://www.nuitka.net/ >`_ is an up-and-coming compiler of Python
390+ into C++ code, aiming to support the full Python language.
391+
392+ Theoretical answer:
393+
394+ .. XXX not sure what to make of this
395+
396+ Not trivially. Python's high level data types, dynamic typing of objects and
384397run-time invocation of the interpreter (using :func: `eval ` or :func: `exec `)
385- together mean that a "compiled" Python program would probably consist mostly of
386- calls into the Python run-time system, even for seemingly simple operations like
387- ``x+1 ``.
398+ together mean that a naïvely "compiled" Python program would probably consist
399+ mostly of calls into the Python run-time system, even for seemingly simple
400+ operations like ``x+1 ``.
388401
389402Several projects described in the Python newsgroup or at past `Python
390403conferences <http://python.org/community/workshops/> `_ have shown that this
@@ -395,34 +408,6 @@ speedups of 1000x are feasible for small demo programs. See the proceedings
395408from the `1997 Python conference
396409<http://python.org/workshops/1997-10/proceedings/> `_ for more information.)
397410
398- Internally, Python source code is always translated into a bytecode
399- representation, and this bytecode is then executed by the Python virtual
400- machine. In order to avoid the overhead of repeatedly parsing and translating
401- modules that rarely change, this byte code is written into a file whose name
402- ends in ".pyc" whenever a module is parsed. When the corresponding .py file is
403- changed, it is parsed and translated again and the .pyc file is rewritten.
404-
405- There is no performance difference once the .pyc file has been loaded, as the
406- bytecode read from the .pyc file is exactly the same as the bytecode created by
407- direct translation. The only difference is that loading code from a .pyc file
408- is faster than parsing and translating a .py file, so the presence of
409- precompiled .pyc files improves the start-up time of Python scripts. If
410- desired, the Lib/compileall.py module can be used to create valid .pyc files for
411- a given set of modules.
412-
413- Note that the main script executed by Python, even if its filename ends in .py,
414- is not compiled to a .pyc file. It is compiled to bytecode, but the bytecode is
415- not saved to a file. Usually main scripts are quite short, so this doesn't cost
416- much speed.
417-
418- .. XXX check which of these projects are still alive
419-
420- There are also several programs which make it easier to intermingle Python and C
421- code in various ways to increase performance. See, for example, `Cython
422- <http://cython.org/> `_, `Pyrex
423- <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/> `_ and `Weave
424- <http://www.scipy.org/Weave> `_.
425-
426411
427412How does Python manage memory?
428413------------------------------
0 commit comments