Skip to content

Commit

Permalink
Merge pull request #2 from phihag/patch-2
Browse files Browse the repository at this point in the history
Clarify Python 2+3 targeting chapter
  • Loading branch information
yasoob committed Aug 17, 2015
2 parents 44c6dd4 + 571fd5e commit 4748338
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions targeting_python_2_3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ in Python 2 you can import it from ``__future__``.
# Output:
from __future__ import print_function
print
print(print)
# Output: <built-in function print>
**Using ``as`` in imports**
**Dealing with module renaming**

First tell me how you import packages in your script ? Most of us do
this :
Expand All @@ -64,9 +64,9 @@ code below :
.. code:: python
try:
import urllib.request as urllib_request #for python 3
import urllib.request as urllib_request # for python 3
except ImportError:
import urllib2 as urllib_request # for python 2
import urllib2 as urllib_request # for python 2
So let me explain the above code a little. We are wrapping our importing
code in a try except clause. We are doing it because in python2 there is
Expand Down Expand Up @@ -111,6 +111,7 @@ functionality in Python 2. For instance we have:
- singledispatch ``pip install singledispatch``
- pathlib ``pip install pathlib``


I am sure there are a lot of other methods and tricks which can be used
to make you code compatible with both of these Python series. This was
just to give you some ideas.

0 comments on commit 4748338

Please sign in to comment.