1. Changing the Rules a Bit
Python 3.0 sees several significant changes in the Python language. The differences vary in import. Some, however, are so pervasive that your 2.x coding style will almost certainly need changing to fit with the 3.0 interpreter. From print as a function to the increased use of iterators to name changes in built-in functions and library calls, Python 3.0 offers a lot of power via an even cleaner version of Python. Here is a guide to help you make the transition from the 2.x world to 3.0.2. Print as a Function
Echoing output to screen is now a function that requires one argument but that will accept up to four. The function's syntax, with default values for the optional arguments, is as follows: print(value, sep=' ', end='\n', file=sys.stdout)
Examples:
Basic usage:
>>> print("Hello, World!")
Hello, World!
With custom separator, line terminator, and file handle:
>>> import sys >>> x = "World"
>>> print("Hello", x, sep = ", ", end="!\n", file=sys.stderr)
Hello, World! 3. Dictionary Lists to Views
Several methods of the dictionary class that return a list under 2.5 and before now return a view. Under Pytho <=2.5, the value returned reflected the dictionary items at the time the method was called. If the dictionary changed immediately afterward, the list did not change. Views allow for dynamic assignment of dictionary items (or their sundry aspects). So, the values refleced in the calls will change with the dictionary. For most programming, this difference will not matter greatly. For example, see the following dictionary containing some of this year's Nobel Prize winners: >>> dict = {"1953":"Paul Krugman", "1921":"Yoichiro Nambu", "1944":"Makoto Kobayashi", "1940":"Toshihide Maskawa", "1928":"Osamu Shimomura", "1947":"Martin Chalfie", "1952":"Roger Y. Tsien"}
>>> keys = sorted(dict)
>>> for year in keys:
... print(dict[year], year, sep=": ")
...
Yoichiro Nambu: 1921
Osamu Shimomura: 1928
Toshihide Maskawa: 1940
Makoto Kobayashi: 1944
Martin Chalfie: 1947
Roger Y. Tsien: 1952
Paul Krugman: 1953
>>> dict["1937"]="Martti Ahtisaari"
>>> for year in keys:
... print(dict[year], year, sep=": ")
...
Yoichiro Nambu: 1921
Osamu Shimomura: 1928
Toshihide Maskawa: 1940
Makoto Kobayashi: 1944
Martin Chalfie: 1947
Roger Y. Tsien: 1952
Paul Krugman: 1953
Note that we have not re-assigned the new list. Once we re-assign keys, however, the output of the loop changes.
>>> keys = sorted(dict)
>>> for year in keys:
... print(dict[year], year, sep=": ")
...
Yoichiro Nambu: 1921
Osamu Shimomura: 1928
Martti Ahtisaari: 1937
Toshihide Maskawa: 1940
Makoto Kobayashi: 1944
Martin Chalfie: 1947
Roger Y. Tsien: 1952
Paul Krugman: 1953
Dictionary methods that are now returning views instead of lists are dict.keys(), dict.items() and dict.values(). Also, some are no longer supported: dict.iterkeys(), dict.iteritems(), and dict.itervalues().4. From Lists and Tuples to Iterators
Additionally, other Python functions that used to return lists now return iterators. This helps to save on system resources in that the iterator is not a static list of items (and so consuming RAM, etc.) but rather generates the next item in a series starting from the value of the last item issued. If the iterator has not been used before, it naturally starts with the first item.
In Python 3.0, map() and filter() return iterators, not lists. A list object can be created quickly, however, by passing the call through list(): list(map(x)).
Also, range() now returns an iterator like xrange() did in 2.5 and before. xrange() no longer exists in 3.0.
Finally, zip() no longer returns a list of tuples but an iterator that returns tuples.
5. Full Unicode Support
With Python 3.0, all strings are stored as Unicode by default. For normal ASCII text, one need not do anything extraordinary; ASCII strings do not look any different than in Python 2.x. However, accented letters or non-Roman typefaces require special handling.
In previous versions of Python, one needed to specify a Unicode tag for the string. In 3.0, one can add non-Roman Unicode on-the-fly. There are three ways to do this: use the character's name, use the 16-bit hex value, or use the 32-bit hex value.
>>> "\N{GREEK CAPITAL LETTER THETA}" # If you know the character name
'Θ'
>>> "\u0398" # This is the 16-bit value; note the lowercase 'u'
'Θ'
>>> "\U00000398" # 32-bit hex value; note the uppercase 'U'
'Θ'
6. When Text is not Data
With this new support comes a very important distinction. Python 3.0 handles strings differently depending on whether the string is encoded. Unencoded strings are viewed as text and are handled as type str. Encoded strings, however, are binary data and are handled as type bytes.
The new Unicode default is underscored by the discontinuance of the u"..." literal for Unicode text. Instead, one must now use b"..." literals. The latter indicates overtly that the string in question is to be treated as encoded data.
>>> y = b"Hello World!"
>>> type(y)
<class 'bytes'>
>>> y = "Hello World!"
>>> type(y)
<class 'str'>
For more on managing the text and data differentiation in Python 3.0,
see "A Guide to Text vs Data in Python 3.0".
