You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
``future`` is the missing compatibility layer between Python 2 and Python 3. It allows you to use a single, clean Python 3.x-compatible codebase to support both Python 2 and Python 3 with minimal overhead.
5
+
``future`` is the missing compatibility layer between Python 3 and Python
6
+
2. It allows you to use a single, clean Python 3.x-compatible codebase to
7
+
support both Python 3 and Python 2 with minimal overhead.
6
8
7
9
Notable projects that use ``future`` for Python 2/3 compatibility are `Mezzanine <http://mezzanine.jupo.org/>`_ and `xlwt-future <https://pypi.python.org/pypi/xlwt-future>`_.
8
10
9
11
It is designed to be used as follows::
10
12
11
13
from __future__ import (absolute_import, division,
12
14
print_function, unicode_literals)
13
-
from future import standard_library
14
-
from future.builtins import * # or explicit imports: str, int, bytes,
15
-
# open, super, range, zip, input, etc.
16
-
17
-
followed by predominantly standard, idiomatic Python 3 code that then runs similarly on Python 2.6/2.7 and Python 3.3+.
15
+
from future import *
18
16
19
-
On Python 3, the import lines have zero effect (and zero namespace
20
-
pollution).
17
+
or with explicit imports::
21
18
22
-
On Python 2, ``from future import standard_library`` installs
23
-
import hooks to allow renamed and moved standard library modules to be
24
-
imported from their new Py3 locations. On Python 2, the ``from future.builtins import *`` line shadows all builtins with different behaviour in Python 3 versus 2 to provide their Python 3 semantics.
19
+
from future.builtins import (filter, map, zip,
20
+
ascii, chr, hex, input, oct, open,
21
+
bytes, int, range, round, str, super)
22
+
23
+
followed by predominantly standard, idiomatic Python 3 code that then runs
24
+
similarly on Python 2.6/2.7 and Python 3.3+.
25
+
26
+
On Python 3, the import lines have zero effect.
27
+
28
+
On Python 2, the import lines shadow all the builtins with different
29
+
behaviour in Python 3 versus 2 to provide their Python 3 semantics.
30
+
31
+
32
+
Standard library reorganization
33
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
+
35
+
``from future import standard_library`` provides a context-manager called
36
+
``enable_hooks`` that installs import hooks (PEP 3108) to allow renamed and
37
+
moved standard library modules to be imported from their new Py3 locations.
25
38
26
39
27
40
Automatic conversion
28
41
--------------------
29
-
An included script called `futurize <http://python-future.org/automatic_conversion.html>`_ aids in converting code (from either Python 2 or Python 3) to code compatible with both platforms. It is similar to ``python-modernize`` but goes further in providing Python 3 compatibility through the use of the backported types and builtin functions in ``future``.
42
+
An included script called `futurize
43
+
<http://python-future.org/automatic_conversion.html>`_ aids in converting
44
+
code (from either Python 2 or Python 3) to code compatible with both
45
+
platforms. It is similar to ``python-modernize`` but goes further in
46
+
providing Python 3 compatibility through the use of the backported types
0 commit comments