A PDF version is here: http://python-future.org/compatible_idioms.pdf
This notebook shows you idioms for writing future-proof code that is compatible with both versions of Python: 2 and 3. It accompanies Ed Schofield’s talk at PyCon AU 2014, “Writing 2/3 compatible code”. (The video is here: http://www.youtube.com/watch?v=KOqk8j11aAI&t=10m14s.)
Minimum versions:
The imports below refer to these pip-installable packages on PyPI:
import future # pip install future
import builtins # pip install future
import past # pip install future
import six # pip install six
The following scripts are also pip-installable:
futurize # pip install future
pasteurize # pip install future
See http://python-future.org and https://pythonhosted.org/six/ for more information.
To print multiple strings, import print_function to prevent Py2 from interpreting it as a tuple:
Integer division (rounding down):
“True division” (float division):
“Old division” (i.e. compatible with Py2 behaviour):
Short integers are gone in Python 3 and long has become int (without the trailing L in the repr).
To test whether a value is an integer (of any kind):
If you are upgrading an existing Python 2 codebase, it may be preferable to mark up all string literals as unicode explicitly with u prefixes:
The futurize and python-modernize tools do not currently offer an option to do this automatically.
If you are writing code for a new project or new codebase, you can use this idiom to make all string literals in a module unicode strings:
See http://python-future.org/unicode_literals.html for more discussion on which style to use.
To loop over a byte-string with possible high-bit characters, obtaining each character as a byte-string of length 1:
As an alternative, chr() and .encode('latin-1') can be used to convert an int into a 1-char byte string:
Suppose the package is:
mypackage/
__init__.py
submodule1.py
submodule2.py
and the code below is in submodule1.py:
Iterable dict keys:
Iterable dict values:
Iterable dict items:
Unicode string: 孔子
As above with zip and itertools.izip.
As above with filter and itertools.ifilter too.
Warning: using either of these is unsafe with untrusted input.
urllib is the hardest module to use from Python 2/3 compatible code. You may like to use Requests (http://python-requests.org) instead.