Skip to content

Commit 571fd5e

Browse files
committed
Clarify Python 2+3 targeting chapter
* Make `print` code work and easier to understand by actually calling print * Fix heading that contained Markdown * Make sure code is PEP8-compliant. It would be nice to give ipaddress a mention as well at the bottom.
1 parent c83d39d commit 571fd5e

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

targeting_python_2_3.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ in Python 2 you can import it from ``__future__``.
3737
# Output:
3838
3939
from __future__ import print_function
40-
print
40+
print(print)
4141
# Output: <built-in function print>
4242
43-
**Using ``as`` in imports**
43+
**Dealing with module renaming**
4444

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

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

0 commit comments

Comments
 (0)