@@ -8,28 +8,25 @@ Just imagine that you have a very popular Python module which is use by
88hundreds of people but not all of them have Python 2 or 3. In that case
99you have two choices. The first one is to distribute 2 modules, one for
1010Python 2 and the other for Python 3. The other choice is to modify your
11- current code and make is compatible with both Python 2 and 3.
11+ current code and make it compatible with both Python 2 and 3.
1212
1313In this section I am going to highlight some of the tricks which you can
1414employ to make a script compatible with both of them.
1515
1616**Future imports **
1717
1818The first and most important method is to use ``__future__ `` imports. It
19- allows you to import Python 3 functionality in Python 2. Here is an
20- example :
19+ allows you to import Python 3 functionality in Python 2. Here are a couple
20+ examples :
2121
22- - Context managers were new in Python 2.6+. For using them in Python 2.5
23- you can use:
22+ Context managers were new in Python 2.6+. For using them in Python 2.5 you can use:
2423
2524.. code :: python
2625
2726 from __future__ import with_statement
2827
29- - ``print `` function
30-
3128 ``print `` was changed to a function in Python 3. If you want to use it
32- in Python 2 you can import it from ``__future__ ``.
29+ in Python 2 you can import it from ``__future__ ``:
3330
3431.. code :: python
3532
@@ -42,7 +39,7 @@ in Python 2 you can import it from ``__future__``.
4239
4340 **Dealing with module renaming **
4441
45- First tell me how you import packages in your script ? Most of us do
42+ First, tell me how you import packages in your script ? Most of us do
4643this :
4744
4845.. code :: python
@@ -69,31 +66,31 @@ code below :
6966 import urllib2 as urllib_request # for Python 2
7067
7168 So let me explain the above code a little. We are wrapping our importing
72- code in a try except clause. We are doing it because in Python 2 there is
73- no ``urllib.request `` module and will result in an ImportError. The
74- functionality of ``urllib.request `` is provided by ``urllib2 `` module in
75- Python 2. So now when in Python 2 we try to import ``urllib.request `` and
76- get an ``ImportError `` we tell Python to import ``urllib2 `` instead.
69+ code in a `` try/ except `` clause. We are doing it because in Python 2 there is
70+ no ``urllib.request `` module so this would result in an `` ImportError `` . The
71+ functionality of ``urllib.request `` is provided by the ``urllib2 `` module in
72+ Python 2. So, when using Python 2, we try to import ``urllib.request `` and
73+ if we get an ``ImportError `` then we tell Python to import ``urllib2 `` instead.
7774
7875The final thing you need to know about is the ``as `` keyword. It is
79- mapping the imported module to ``urllib_request ``. So that now all of
80- the Classes and methods of ``urllib2 `` are available to us by
76+ mapping the imported module to ``urllib_request ``. So that all of
77+ the classes and methods within ``urllib2 `` are available to us via the alias
8178``urllib_request ``.
8279
8380**Obsolete Python 2 builtins **
8481
8582Another thing to keep in mind is that there are 12 Python 2 builtins
8683which have been removed from Python 3. Make sure that you don't use them
87- in Python 2 as well in order to make your code compatible with Python 3.
88- Here is a way to enforce you to abandon these 12 builtins in Python 2 as
89- well.
84+ in Python 2 in order to make your code compatible with Python 3.
85+ Here is a way to enforce that you abandon these 12 builtins in Python 2 as
86+ well:
9087
9188.. code :: python
9289
9390 from future.builtins.disabled import *
9491
9592 Now whenever you try to use the modules which are abandoned in Python 3,
96- it raises a NameError like this:
93+ it raises a `` NameError `` like this:
9794
9895.. code :: python
9996
@@ -105,7 +102,7 @@ it raises a NameError like this:
105102 **External standard-library backports **
106103
107104There are a few packages in the wild which provide Python 3
108- functionality in Python 2. For instance we have:
105+ functionality in Python 2. For instance, we have:
109106
110107- enum ``pip install enum34 ``
111108- singledispatch ``pip install singledispatch ``
0 commit comments