codeop.rst 2.95 KB

:mod:`codeop` --- Compile Python code

Source code: :source:`Lib/codeop.py`


The :mod:`codeop` module provides utilities upon which the Python read-eval-print loop can be emulated, as is done in the :mod:`code` module. As a result, you probably don't want to use the module directly; if you want to include such a loop in your program you probably want to use the :mod:`code` module instead.

There are two parts to this job:

  1. Being able to tell if a line of input completes a Python statement: in short, telling whether to print '>>>' or '...' next.
  2. Remembering which future statements the user has entered, so subsequent input can be compiled with these in effect.

The :mod:`codeop` module provides a way of doing each of these things, and a way of doing them both.

To do just the former:

Instances of this class have :meth:`__call__` methods identical in signature to the built-in function :func:`compile`, but with the difference that if the instance compiles program text containing a :mod:`__future__` statement, the instance 'remembers' and compiles all subsequent program texts with the statement in force.

Instances of this class have :meth:`__call__` methods identical in signature to :func:`compile_command`; the difference is that if the instance compiles program text containing a __future__ statement, the instance 'remembers' and compiles all subsequent program texts with the statement in force.