diff --git a/porting-code-to-python-3-with-2to3.html b/porting-code-to-python-3-with-2to3.html index 7d64e62..ae0750c 100644 --- a/porting-code-to-python-3-with-2to3.html +++ b/porting-code-to-python-3-with-2to3.html @@ -219,7 +219,7 @@ import CGIHttpServer
http.server module provides a basic HTTP server.
urllibPython 2 had a rat’s nest of overlapping modules to parse, encode, and fetch URLs. In Python 3, these have all been refactored and combined in a single package, urllib.
+
Python 2 had a rat’s nest of overlapping modules to parse, encode, and fetch URLs. In Python 3, these have all been refactored and combined in a single package, urllib.
| Notes | Python 2 @@ -249,10 +249,10 @@ from urllib.error import HTTPError |
|---|
urllib module in Python 2 had a variety of functions, including urlopen() for fetching data and splittype(), splithost(), and splituser() for splitting a URL into its constituent parts. These functions have been reorganized more logically within the new urllib package. 2to3 will also change all calls to these functions so they use the new naming scheme.
-urllib2 module in Python 2 has been folded into into the urllib package in Python 3. All your urllib2 favorites — the build_opener() method, Request objects, and HTTPBasicAuthHandler and friends — are still available.
+urllib2 module in Python 2 has been folded into the urllib package in Python 3. All your urllib2 favorites — the build_opener() method, Request objects, and HTTPBasicAuthHandler and friends — are still available.
urllib.parse module in Python 3 contains all the parsing functions from the old urlparse module in Python 2.
urllib.robotparser module parses robots.txt files.
-FancyURLopener class, which handles HTTP redirects and other status codes, is still available in the new urllib.request module. The urlencode function has moved to urllib.parse.
+FancyURLopener class, which handles HTTP redirects and other status codes, is still available in the new urllib.request module. The urlencode() function has moved to urllib.parse.
Request object is still available in urllib.request, but constants like HTTPError have been moved to urllib.error.
dbmnext() function. (The 2to3 script is smart enough to convert this properly.)
__next__() special method.
next() that takes one or more arguments, 2to3 will not touch it. This class can not be used as an iterator, because its next() method takes arguments.
-next() function. In this case, you need to call the iterator’s special __next()__ method to get the next item in the sequence. (Alternatively, you could also refactor the code so the local variable wasn’t named next, but 2to3 will not do that for you automatically.)
+next() function. In this case, you need to call the iterator’s special __next__() method to get the next item in the sequence. (Alternatively, you could also refactor the code so the local variable wasn’t named next, but 2to3 will not do that for you automatically.)
filter() global functionIn Python 2, the filter() function returned a list, the result of filtering a sequence through a function that returned True or False for each item in the sequence. In Python 3, the filter() function returns an iterator, not a list.
@@ -478,7 +478,7 @@ for an_iterator in a_sequence_of_iterators:
from functtools import reduce
reduce(a, b, c)
-+☞The version of
2to3that shipped with Python 3.0 would not fix thereduce()function automatically. The fix first appeared in the2to3script that shipped with Python 3.1.@@ -547,7 +547,7 @@ reduce(a, b, c)
apply()global functionexecfile("a_filename")exec(compile(open("a_filename").read(), "a_filename", "exec"))-+☞The version of
2to3that shipped with Python 3.0 would not fix theexecfilestatement automatically. The fix first appeared in the2to3script that shipped with Python 3.1.@@ -805,7 +805,7 @@ except:
reprliterals (backticks)aClassInstance.aClassMethod.im_classaClassInstance.aClassMethod.self.__class__-+
__nonzero__special class attribute
__nonzero__special methodIn Python 2, you could build your own classes that could be used in a boolean context. For example, you could instantiate the class and then use the instance in an
ifstatement. To do this, you defined a special__nonzero__()method which returnedTrueorFalse, and it was called whenever the instance was used in a boolean context. In Python 3, you can still do this, but the name of the method has changed to__bool__().+
Notes @@ -920,6 +920,9 @@ except: types.NoneTypetype(None)+☞
types.StringTypegets mapped tobytesinstead ofstrbecause a Python 2 “string” (not a Unicode string, just a regular string) is really just a sequence of bytes in a particular character encoding. +
isinstance()global function (3.1+)The
isinstance()function checks whether an object is an instance of a particular class or type. In Python 2, you could pass a tuple of types, andisinstance()would returnTrueif the object was any of those types. In Python 3, you can still do this, but passing the same type twice is deprecated.@@ -930,7 +933,7 @@ except:
-isinstance(x, (int, float, int))isinstance(x, (int, float))+☞The version of
2to3that shipped with Python 3.0 would not fix these cases ofisinstance()automatically. The fix first appeared in the2to3script that shipped with Python 3.1.diff --git a/table-of-contents.html b/table-of-contents.html index 016747a..0a31f44 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -339,7 +339,7 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
basestringdatatypexreadlines()I/O methodlambdafunctions that take a tuple instead of multiple parametersSpecial method attributes - __nonzero__special class attribute +__nonzero__special methodOctal literals sys.maxintcallable()global function