##// END OF EJS Templates
Document single codebase Python 3 compatibility
Thomas Kluyver -
Show More
@@ -0,0 +1,122 b''
1 Writing code for Python 2 and 3
2 ===============================
3
4 Iterators
5 ---------
6
7 Many built in functions and methods in Python 2 come in pairs, one
8 returning a list, and one returning an iterator (e.g. :func:`range` and
9 :func:`xrange`). In Python 3, there is usually only the iterator form,
10 but it has the name which gives a list in Python 2 (e.g. :func:`range`).
11
12 The way to write compatible code depends on what you need:
13
14 * A list, e.g. for serialisation, or to test if something is in it.
15 * Iteration, but it will never be used for very many items, so efficiency
16 isn't especially important.
17 * Iteration over many items, where efficiency is important.
18
19 ================ ================= =======================
20 list iteration (small) iteration(large)
21 ================ ================= =======================
22 list(range(n)) range(n) py3compat.xrange(n)
23 list(map(f, it)) map(f, it) --
24 list(zip(a, b)) zip(a, b) --
25 list(d.items()) d.items() py3compat.iteritems(d)
26 list(d.values()) d.values() py3compat.itervalues(d)
27 ================ ================= =======================
28
29 Iterating over a dictionary yields its keys, so there is rarely a need
30 to use :meth:`dict.keys` or :meth:`dict.iterkeys`.
31
32 Avoid using :func:`map` to cause function side effects. This is more
33 clearly written with a simple for loop.
34
35 Changed standard library locations
36 ----------------------------------
37
38 Several parts of the standard library have been renamed and moved. This
39 is a short list of things that we're using. A couple of them have names
40 in :mod:`IPython.utils.py3compat`, so you don't need both
41 imports in each module that uses them.
42
43 ================== ============ ===========
44 Python 2 Python 3 py3compat
45 ================== ============ ===========
46 :func:`raw_input` input input
47 :mod:`__builtin__` builtins builtin_mod
48 :mod:`StringIO` io
49 :mod:`Queue` queue
50 :mod:`cPickle` pickle
51 :mod:`thread` _thread
52 :mod:`copy_reg` copyreg
53 :mod:`urlparse` urllib.parse
54 :mod:`repr` reprlib
55 :mod:`Tkinter` tkinter
56 :mod:`Cookie` http.cookie
57 :mod:`_winreg` winreg
58 ================== ============ ===========
59
60 Be careful with StringIO: :class:`io.StringIO` is available in Python 2.7,
61 but it behaves differently from :class:`StringIO.StringIO`, and much of
62 our code assumes the use of the latter on Python 2. So a try/except on
63 the import may cause problems.
64
65 Unicode
66 -------
67
68 Always be explicit about what is text (unicode) and what is bytes.
69 *Encoding* goes from unicode to bytes, and *decoding* goes from bytes
70 to unicode.
71
72 To open files for reading or writing text, use :func:`io.open`, which is
73 the Python 3 builtin ``open`` function, available on Python 2 as well.
74 We almost always need to specify the encoding parameter, because the
75 default is platform dependent.
76
77 Relative imports
78 ----------------
79
80 ::
81
82 # This makes Python 2 behave like Python 3:
83 from __future__ import absolute_import
84
85 import io # Imports the standard library io module
86 from . import io # Import the io module from the package
87 # containing the current module
88 from .io import foo # foo from the io module next to this module
89 from IPython.utils import io # This still works
90
91 Print function
92 --------------
93
94 ::
95
96 # Support the print function on Python 2:
97 from __future__ import print_function
98
99 print(a, b)
100 print(foo, file=sys.stderr)
101 print(bar, baz, sep='\t', end='')
102
103 Metaclasses
104 -----------
105
106 The syntax for declaring a class with a metaclass is different in
107 Python 2 and 3. In most cases, the helper function
108 :func:`~IPython.utils.py3compat.with_metaclass` (copied from the six
109 library) can be used like this::
110
111 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
112 ...
113
114 Combining inheritance between Qt and the traitlets system, however, does
115 not work with this. Instead, we do this::
116
117 class QtKernelClientMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})):
118 ...
119
120 This gives the new class a metaclass of :class:`~IPython.qt.util.MetaQObjectHasTraits`,
121 and the parent classes :class:`~IPython.utils.traitlets.HasTraits` and
122 :class:`~IPython.qt.util.SuperQObject`.
@@ -0,0 +1,8 b''
1 Single codebase Python 3 support
2 --------------------------------
3
4 IPython previously supported Python 3 by running 2to3 during setup. We
5 have now switched to a single codebase which runs natively on Python 2.7
6 and 3.3.
7
8 For notes on how to maintain this, see :doc:`/development/pycompat`.
@@ -46,6 +46,7 b' extensions = ['
46 'sphinx.ext.autodoc',
46 'sphinx.ext.autodoc',
47 'sphinx.ext.doctest',
47 'sphinx.ext.doctest',
48 'sphinx.ext.inheritance_diagram',
48 'sphinx.ext.inheritance_diagram',
49 'sphinx.ext.intersphinx',
49 'IPython.sphinxext.ipython_console_highlighting',
50 'IPython.sphinxext.ipython_console_highlighting',
50 'IPython.sphinxext.ipython_directive',
51 'IPython.sphinxext.ipython_directive',
51 'numpydoc', # to preprocess docstrings
52 'numpydoc', # to preprocess docstrings
@@ -178,6 +179,7 b' html_additional_pages = {'
178 # Output file base name for HTML help builder.
179 # Output file base name for HTML help builder.
179 htmlhelp_basename = 'ipythondoc'
180 htmlhelp_basename = 'ipythondoc'
180
181
182 intersphinx_mapping = {'http://docs.python.org/2/': None}
181
183
182 # Options for LaTeX output
184 # Options for LaTeX output
183 # ------------------------
185 # ------------------------
@@ -24,3 +24,4 b' on the IPython GitHub wiki.'
24 messaging
24 messaging
25 parallel_messages
25 parallel_messages
26 parallel_connections
26 parallel_connections
27 pycompat
General Comments 0
You need to be logged in to leave comments. Login now