pasteurize.rst 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. .. _backwards-conversion:
  2. ``pasteurize``: Py3 to Py2/3
  3. ----------------------------
  4. Running ``pasteurize -w mypy3module.py`` turns this Python 3 code::
  5. import configparser
  6. import copyreg
  7. class Blah:
  8. pass
  9. print('Hello', end=None)
  10. into this code which runs on both Py2 and Py3::
  11. from __future__ import print_function
  12. from future import standard_library
  13. standard_library.install_hooks()
  14. import configparser
  15. import copyreg
  16. class Blah(object):
  17. pass
  18. print('Hello', end=None)
  19. Notice that both ``futurize`` and ``pasteurize`` create explicit new-style
  20. classes that inherit from ``object`` on both Python versions, and both
  21. refer to stdlib modules (as well as builtins) under their Py3 names.
  22. Note also that the ``configparser`` module is a special case; there is a full
  23. backport available on PyPI (https://pypi.python.org/pypi/configparser), so, as
  24. of v0.16.0, ``python-future`` no longer provides a ``configparser`` package
  25. alias. To use the resulting code on Py2, install the ``configparser`` backport
  26. with ``pip install configparser`` or by adding it to your ``requirements.txt``
  27. file.
  28. ``pasteurize`` also handles the following Python 3 features:
  29. - keyword-only arguments
  30. - metaclasses (using :func:`~future.utils.with_metaclass`)
  31. - extended tuple unpacking (PEP 3132)
  32. To handle function annotations (PEP 3107), see :ref:`func_annotations`.