328x Filetype PDF File size 0.28 MB Source: link.springer.com
A P P E N D I X A
■ ■ ■
Porting Code to Python 3 with 2to3
Virtually all Python 2 programs need at least some tweaking to run properly under Python 3. To help
with this transition, Python 3 comes with a utility script called 2to3, which takes your actual Python 2
source code as input and auto-converts as much as it can to Python 3. Chapter 15 described how to run
the 2to3 script and showed some things it can’t fix automatically. This appendix documents what it can
fix automatically.
print Statement
In Python 2, print was a statement. Whatever you wanted to print simply followed the print keyword. In
Python 3, print() is a function. Whatever you want to print, pass it to print() like any other function, as
shown in Table A-1.
Table A-1. The print() Statement
Notes Python 2 Python 3
(1) print print()
(2) print 1 print(1)
(3) print 1, 2 print(1, 2)
(4) print 1, 2, print(1, 2, end=' ')
(5) print >>sys.stderr, 1, 2, 3 print(1, 2, 3, file=sys.stderr)
1. To print a blank line, call print() without any arguments.
2. To print a single value, call print() with one argument.
3. To print two values separated by a space, call print() with two arguments.
295
APPENDIX A ■ PORTING CODE TO PYTHON 3 WITH 2TO3
4. This one is a little tricky. In Python 2, if you ended a print statement with a
comma, it would print the values separated by spaces, print a trailing space,
and then stop without printing a carriage return. In Python 3, the way to do
this is to pass end=' ' as a keyword argument to the print() function. The end
argument defaults to '\n' (a carriage return), so overriding it will suppress the
carriage return after printing the other arguments.
5. In Python 2, you could redirect the output to a pipe (such as sys.stderr) by
using the >>pipe_name syntax. In Python 3, the way to do this is to pass the pipe
in the file keyword argument. The file argument defaults to sys.stdout
(standard out), so overriding it will output to a different pipe instead.
Unicode String Literals
Python 2 had two string types: Unicode strings and non-Unicode strings. Python 3 has one string type:
Unicode strings, as shown in Table A-2.
Table A-2. Unicode String Literals
Notes Python 2 Python 3
(1) u'PapayaWhip' 'PapayaWhip'
(2) ur'PapayaWhip\foo' r'PapayaWhip\foo'
1. Unicode string literals are simply converted into string literals, which are
always Unicode in Python 3.
2. Unicode raw strings (in which Python does not auto-escape backslashes) are
converted to raw strings. In Python 3, raw strings are always Unicode.
unicode() Global Function
Python 2 had two global functions to coerce objects into strings: unicode() to coerce them into Unicode
strings and str() to coerce them into non-Unicode strings. Python 3 has only one string type, Unicode
strings, so the str() function is all you need. (The unicode() function no longer exists.) See Table A-3.
296
APPENDIX A ■ PORTING CODE TO PYTHON 3 WITH 2TO3
Table A-3. The unicode() Global Function
Python 2 Python 3
unicode(anything) str(anything)
long Datatype
Python 2 had separate int and long types for non-floating-point numbers. An int could not be any
larger than sys.maxint, which varied by platform. Longs were defined by appending an L to the end of
the number, and they could be, well, longer than ints. In Python 3, there is only one integer type, called
int, which mostly behaves like the long type in Python 2. Because there are no longer two types, there is
no need for special syntax to distinguish them (see Table A-4).
Table A-4. long Datatypes
Notes Python 2 Python 3
(1) x = 1000000000000L X = 1000000000000
(2) x = 0xFFFFFFFFFFFFL x = 0xFFFFFFFFFFFF
(3) long(x) int(x)
(4) type(x) is long type(x) is int
(5) isinstance(x, long) isinstance(x, int)
1. Base 10 long integer literals become base 10 integer literals.
2. Base 16 long integer literals become base 16 integer literals.
3. In Python 3, the old long() function no longer exists because longs don’t exist.
To coerce a variable to an integer, use the int() function.
4. To check whether a variable is an integer, get its type and compare it with int,
not long.
5. You can also use the isinstance() function to check datatypes; again, use int,
not long, to check for integers.
<> Comparison
Python 2 supported <> as a synonym for !=, the not-equals comparison operator. Python 3 supports the
!= operator, but not <>, as shown in Table A-5.
297
APPENDIX A ■ PORTING CODE TO PYTHON 3 WITH 2TO3
Table A-5. <> Comparison
Notes Python 2 Python 3
(1) if x <> y: if x != y:
(2) if x <> y <> z: if x != y != z:
1. This is a simple comparison between two values.
2. This is a more complex comparison between three values.
has_key() Dictionary Method
In Python 2, dictionaries had a has_key() method to test whether the dictionary had a certain key. In
Python 3, this method no longer exists. Instead, you need to use the in operator. (See Table A-6.)
Table A-6. has_key() Dictionary Method
Notes Python 2 Python 3
(1) a_dictionary.has_key('PapayaWhip') 'PapayaWhip' in a_dictionary
(2) a_dictionary.has_key(x) or x in a_dictionary or y in a_dictionary
a_dictionary.has_key(y)
(3) a_dictionary.has_key(x or y) (x or y) in a_dictionary
(4) a_dictionary.has_key(x + y) (x + y) in a_dictionary
(5) x + a_dictionary.has_key(y) x + (y in a_dictionary)
1. The simplest form.
2. The in operator takes precedence over the or operator, so there is no need for
parentheses here.
3. On the other hand, you do need parentheses here, for the same reason: in
takes precedence over or. Note that this code is completely different from the
previous line. Python interprets x or y first, which results in either x (if x is
true in a Boolean context) or y. Then it takes that singular value and checks
whether it is a key in a_dictionary.
4. The in operator takes precedence over the + operator, so this form technically
doesn’t need parentheses, but 2to3 includes them anyway.
5. This form definitely needs parentheses because the in operator takes
precedence over the + operator.
298
no reviews yet
Please Login to review.