@@ -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,99 +408,64 @@ 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------------------------------
429414
430415The details of Python memory management depend on the implementation. The
431- standard C implementation of Python uses reference counting to detect
432- inaccessible objects, and another mechanism to collect reference cycles,
416+ standard implementation of Python, :term: ` CPython `, uses reference counting to
417+ detect inaccessible objects, and another mechanism to collect reference cycles,
433418periodically executing a cycle detection algorithm which looks for inaccessible
434419cycles and deletes the objects involved. The :mod: `gc ` module provides functions
435420to perform a garbage collection, obtain debugging statistics, and tune the
436421collector's parameters.
437422
438- Jython relies on the Java runtime so the JVM's garbage collector is used. This
439- difference can cause some subtle porting problems if your Python code depends on
440- the behavior of the reference counting implementation.
423+ Other implementations (such as `Jython <http://www.jython.org >`_ or
424+ `PyPy <http://www.pypy.org >`_), however, can rely on a different mechanism
425+ such as a full-blown garbage collector. This difference can cause some
426+ subtle porting problems if your Python code depends on the behavior of the
427+ reference counting implementation.
441428
442- .. XXX relevant for Python 3?
429+ In some Python implementations, the following code (which is fine in CPython)
430+ will probably run out of file descriptors::
443431
444- Sometimes objects get stuck in traceback temporarily and hence are not
445- deallocated when you might expect. Clear the traceback with::
432+ for file in very_long_list_of_files:
433+ f = open(file)
434+ c = f.read(1)
446435
447- import sys
448- sys.last_traceback = None
436+ Indeed, using CPython's reference counting and destructor scheme, each new
437+ assignment to *f * closes the previous file. With a traditional GC, however,
438+ those file objects will only get collected (and closed) at varying and possibly
439+ long intervals.
449440
450- Tracebacks are used for reporting errors, implementing debuggers and related
451- things. They contain a portion of the program state extracted during the
452- handling of an exception (usually the most recent exception).
441+ If you want to write code that will work with any Python implementation,
442+ you should explicitly close the file or use the :keyword: ` with ` statement;
443+ this will work regardless of memory management scheme::
453444
454- In the absence of circularities, Python programs do not need to manage memory
455- explicitly.
445+ for file in very_long_list_of_files:
446+ with open(file) as f:
447+ c = f.read(1)
456448
457- Why doesn't Python use a more traditional garbage collection scheme? For one
458- thing, this is not a C standard feature and hence it's not portable. (Yes, we
459- know about the Boehm GC library. It has bits of assembler code for *most *
460- common platforms, not for all of them, and although it is mostly transparent, it
461- isn't completely transparent; patches are required to get Python to work with
462- it.)
449+
450+ Why doesn't CPython use a more traditional garbage collection scheme?
451+ ---------------------------------------------------------------------
452+
453+ For one thing, this is not a C standard feature and hence it's not portable.
454+ (Yes, we know about the Boehm GC library. It has bits of assembler code for
455+ *most * common platforms, not for all of them, and although it is mostly
456+ transparent, it isn't completely transparent; patches are required to get
457+ Python to work with it.)
463458
464459Traditional GC also becomes a problem when Python is embedded into other
465460applications. While in a standalone Python it's fine to replace the standard
466461malloc() and free() with versions provided by the GC library, an application
467462embedding Python may want to have its *own * substitute for malloc() and free(),
468- and may not want Python's. Right now, Python works with anything that
463+ and may not want Python's. Right now, CPython works with anything that
469464implements malloc() and free() properly.
470465
471- In Jython, the following code (which is fine in CPython) will probably run out
472- of file descriptors long before it runs out of memory::
473-
474- for file in very_long_list_of_files:
475- f = open(file)
476- c = f.read(1)
477-
478- Using the current reference counting and destructor scheme, each new assignment
479- to f closes the previous file. Using GC, this is not guaranteed. If you want
480- to write code that will work with any Python implementation, you should
481- explicitly close the file or use the :keyword: `with ` statement; this will work
482- regardless of GC::
483466
484- for file in very_long_list_of_files:
485- with open(file) as f:
486- c = f.read(1)
487-
488-
489- Why isn't all memory freed when Python exits?
490- ---------------------------------------------
467+ Why isn't all memory freed when CPython exits?
468+ ----------------------------------------------
491469
492470Objects referenced from the global namespaces of Python modules are not always
493471deallocated when Python exits. This may happen if there are circular
@@ -647,10 +625,10 @@ order to remind you of that fact, it does not return the sorted list. This way,
647625you won't be fooled into accidentally overwriting a list when you need a sorted
648626copy but also need to keep the unsorted version around.
649627
650- In Python 2.4 a new built-in function -- :func: `sorted ` -- has been added.
651- This function creates a new list from a provided iterable, sorts it and returns
652- it. For example, here's how to iterate over the keys of a dictionary in sorted
653- order::
628+ If you want to return a new list, use the built-in :func: `sorted ` function
629+ instead. This function creates a new list from a provided iterable, sorts
630+ it and returns it . For example, here's how to iterate over the keys of a
631+ dictionary in sorted order::
654632
655633 for key in sorted(mydict):
656634 ... # do whatever with mydict[key]...
0 commit comments