Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | **************************** |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 2 | What's New In Python 3.0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | **************************** |
| 4 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 5 | .. XXX Add trademark info for Apple, Microsoft. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 7 | .. XXX Remove duplicates; just put info in the most relevant section. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 4a98a2a | 2008-11-21 18:35:43 +0000 | [diff] [blame] | 9 | :Author: Guido van Rossum |
| 10 | :Release: |release| |
| 11 | :Date: |today| |
| 12 | |
| 13 | .. $Id$ |
| 14 | Rules for maintenance: |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 15 | |
| 16 | * Anyone can add text to this document. Do not spend very much time |
| 17 | on the wording of your changes, because your text will probably |
| 18 | get rewritten to some degree. |
| 19 | |
| 20 | * The maintainer will go through Misc/NEWS periodically and add |
| 21 | changes; it's therefore more important to add your changes to |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 22 | Misc/NEWS than to this file. (Note: I didn't get to this for 3.0. |
| 23 | GvR.) |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 24 | |
| 25 | * This is not a complete list of every single change; completeness |
| 26 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 27 | or esoteric to include. If such a change is added to the text, |
| 28 | I'll just remove it. (This is another reason you shouldn't spend |
| 29 | too much time on writing your addition.) |
| 30 | |
| 31 | * If you want to draw your new text to the attention of the |
| 32 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 33 | section. |
| 34 | |
| 35 | * It's OK to just add a fragmentary note about a change. For |
| 36 | example: "XXX Describe the transmogrify() function added to the |
| 37 | socket module." The maintainer will research the change and |
| 38 | write the necessary text. |
| 39 | |
| 40 | * You can comment out your additions if you like, but it's not |
| 41 | necessary (especially when a final release is some months away). |
| 42 | |
| 43 | * Credit the author of a patch or bugfix. Just the name is |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 44 | sufficient; the e-mail address isn't necessary. (Due to time |
| 45 | constraints I haven't managed to do this for 3.0. GvR.) |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 46 | |
| 47 | * It's helpful to add the bug/patch number as a comment: |
| 48 | |
| 49 | % Patch 12345 |
| 50 | XXX Describe the transmogrify() function added to the socket |
| 51 | module. |
| 52 | (Contributed by P.Y. Developer.) |
| 53 | |
| 54 | This saves the maintainer the effort of going through the SVN log |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 55 | when researching a change. (Again, I didn't get to this for 3.0. |
| 56 | GvR.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 58 | This article explains the new features in Python 3.0, compared to 2.6. |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 59 | Python 3.0, also known as "Python 3000" or "Py3K", is the first ever |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 60 | *intentionally incompatible* release. There are more changes than in |
| 61 | a typical release, and more that are important for all Python users. |
| 62 | Nevertheless, after digesting the changes, you'll find that Python |
| 63 | really hasn't changed all that much -- by and large, we're merely |
| 64 | fixing well-known annoyances and warts. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 65 | |
| 66 | This article doesn't attempt to provide a complete specification of |
| 67 | the new features, but instead provides a convenient overview. For |
| 68 | full details, you should refer to the documentation for Python 3.0. If |
| 69 | you want to understand the complete implementation and design |
| 70 | rationale, refer to the PEP for a particular new feature. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 72 | .. Compare with previous release in 2 - 3 sentences here. |
| 73 | .. add hyperlink when the documentation becomes available online. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 75 | .. ====================================================================== |
| 76 | .. Large, PEP-level features and changes should be described here. |
| 77 | .. Should there be a new section here for 3k migration? |
| 78 | .. Or perhaps a more general section describing module changes/deprecation? |
| 79 | .. sets module deprecated |
| 80 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 83 | Common Stumbling Blocks |
| 84 | ======================= |
| 85 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 86 | This section lists those few changes that are most likely to trip you |
| 87 | up if you're used to Python 2.5. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 89 | Print Is A Function |
| 90 | ------------------- |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 92 | The :keyword:`print` statement has been replaced with a :func:`print` |
| 93 | function, with keyword arguments to replace most of the special syntax |
| 94 | of the old :keyword:`print` statement (:pep:`3105`). Examples:: |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 96 | Old: print "The answer is", 2*2 |
| 97 | New: print("The answer is", 2*2) |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 99 | Old: print x, # Trailing comma suppresses newline |
| 100 | New: print(x, end=" ") # Appends a space instead of a newline |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 102 | Old: print # Prints a newline |
| 103 | New: print() # You must call the function! |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 105 | Old: print >>sys.stderr, "fatal error" |
| 106 | New: print("fatal error", file=sys.stderr) |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 108 | Old: print (x, y) # prints repr((x, y)) |
| 109 | New: print((x, y)) # Not the same as print(x, y)! |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 111 | You can also customize the separator between items, e.g.:: |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 113 | print("There are <", 2**32, "> possibilities!", sep="") |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 115 | which produces:: |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 117 | There are <4294967296> possibilities! |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 119 | Note: |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 121 | * The :func:`print` function doesn't support the "softspace" feature of |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 122 | the old :keyword:`print` statement. For example, in Python 2.x, |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 123 | ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0, |
| 124 | ``print("A\n", "B")`` writes ``"A\n B\n"``. |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 126 | * Initially, you'll be finding yourself typing the old ``print x`` |
| 127 | a lot in interactive mode. Time to retrain your fingers to type |
| 128 | ``print(x)`` instead! |
Guido van Rossum | dff1c31 | 2007-09-06 14:46:41 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 130 | * When using the ``2to3`` source-to-source conversion tool, all |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 131 | :keyword:`print` statements are automatically converted to |
| 132 | :func:`print` function calls, so this is mostly a non-issue for |
| 133 | larger projects. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 135 | Text Vs. Data Instead Of Unicode Vs. 8-bit |
| 136 | ------------------------------------------ |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 138 | Everything you thought you knew about binary data and Unicode has |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 139 | changed: |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 141 | XXX HIRO |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 143 | * Python 3.0 uses the concepts of *text* and (binary) *data* instead |
| 144 | of Unicode strings and 8-bit strings. All text is Unicode; however |
| 145 | *encoded* Unicode is represented as binary data. The type used to |
| 146 | hold text is :class:`str`, the type used to hold data is |
| 147 | :class:`bytes`. The difference is that any attempt to mix text and |
| 148 | data in Python 3.0 raises a TypeError exception, whereas if you were |
| 149 | to mix Unicode and 8-bit strings in Python 2.x, you would only get |
| 150 | an exception if the 8-bit string contained non-ASCII values. As a |
| 151 | consequence, pretty much all code that uses Unicode, encodings or |
| 152 | binary data most likely has to change. The change is for the |
| 153 | better, as in the 2.x world there were numerous bugs having to do |
| 154 | with mixing encoded and unencoded text. |
| 155 | |
| 156 | * You no longer use ``u"..."`` literals for Unicode text. However, |
| 157 | you must use ``b"..."`` literals for binary data. |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 159 | * Files opened as text files (still the default mode for :func:`open`) |
| 160 | always use an encoding to map between strings (in memory) and bytes |
| 161 | (on disk). Binary files (opened with a ``b`` in the mode argument) |
| 162 | always use bytes in memory. This means that if a file is opened |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 163 | using an incorrect mode or encoding, I/O will likely fail. It also |
| 164 | means that even Unix users will have to specify the correct mode |
| 165 | (text or binary) when opening a file. There is a platform-dependent |
| 166 | default encoding, which on Unixy platforms can be set with the |
| 167 | ``LANG`` environment variable (and sometimes also with some other |
| 168 | platform-specific locale-related environment variables). In many |
| 169 | cases, but not all, the system default is UTF-8; you should never |
| 170 | count on this default. Any application reading or writing more than |
| 171 | pure ASCII text should probably have a way to override the encoding. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 173 | * The builtin :class:`basestring` abstract type was removed. Use |
| 174 | :class:`str` instead. The :class:`str` and :class:`bytes` types |
| 175 | don't have functionality enough in common to warrant a shared base |
| 176 | class. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 178 | * Filenames are passed to and returned from APIs as (Unicode) strings. |
| 179 | This can present platform-specific problems because on some |
| 180 | platforms filenames are arbitrary byte strings. (On the other hand |
| 181 | on Windows, filenames are natively stored as Unicode.) As a |
| 182 | work-around, most APIs (e.g. :func:`open` and many functions in the |
| 183 | :mod:`os` module) that take filenames accept :class:`bytes` objects |
| 184 | as well as strings, and a few APIs have a way to ask for a |
| 185 | :class:`bytes` return value: :func:`os.listdir` returns a |
| 186 | :class:`bytes` instance if the argument is a :class:`bytes` |
| 187 | instance, and :func:`os.getcwdu` returns the current working |
| 188 | directory as a :class:`bytes` instance. |
| 189 | |
| 190 | * Some system APIs like :data:`os.environ` and :data:`sys.argv` can |
| 191 | also present problems when the bytes made available by the system is |
| 192 | not interpretable using the default encoding. Setting the ``LANG`` |
| 193 | variable and rerunning the program is probably the best approach. |
| 194 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 195 | * All backslashes in raw strings are interpreted literally. This |
| 196 | means that ``'\U'`` and ``'\u'`` escapes in raw strings are not |
| 197 | treated specially. |
| 198 | |
| 199 | XXX Deal with dupes below |
| 200 | |
| 201 | * There is only one text string type; its name is :class:`str` but its |
| 202 | behavior and implementation are like :class:`unicode` in 2.x. |
| 203 | |
| 204 | * The :class:`basestring` superclass has been removed. The ``2to3`` |
| 205 | tool (see below) replaces every occurrence of :class:`basestring` |
| 206 | with :class:`str`. |
| 207 | |
| 208 | * :pep:`3137`: There is a new type, :class:`bytes`, to represent |
| 209 | binary data (and encoded text, which is treated as binary data until |
| 210 | it is decoded). The :class:`str` and :class:`bytes` types cannot be |
| 211 | mixed; you must always explicitly convert between them, using the |
| 212 | :meth:`str.encode` (str -> bytes) or :meth:`bytes.decode` (bytes -> |
| 213 | str) methods. |
| 214 | |
| 215 | * Like :class:`str`, the :class:`bytes` type is immutable. There is a |
| 216 | separate *mutable* type to hold buffered binary data, |
| 217 | :class:`bytearray`. Nearly all APIs that accept :class:`bytes` also |
| 218 | accept :class:`bytearray`. The mutable API is based on |
| 219 | :class:`collections.MutableSequence`. |
| 220 | |
| 221 | * :pep:`3138`: The :func:`repr` of a string no longer escapes |
| 222 | non-ASCII characters. It still escapes control characters and code |
| 223 | points with non-printable status in the Unicode standard, however. |
| 224 | |
| 225 | * :pep:`3120`: The default source encoding is now UTF-8. |
| 226 | |
| 227 | * :pep:`3131`: Non-ASCII letters are now allowed in identifiers. |
| 228 | (However, the standard library remains ASCII-only with the exception |
| 229 | of contributor names in comments.) |
| 230 | |
| 231 | * :pep:`3116`: New I/O implementation. The API is nearly 100% |
| 232 | backwards compatible, but completely reimplemented (currently largely |
| 233 | in Python). Also, binary files use bytes instead of strings. |
| 234 | |
| 235 | * The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead, |
| 236 | import :class:`io.StringIO` or :class:`io.BytesIO`, for text and |
| 237 | data respectively. |
| 238 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 239 | * See also the :ref:`unicode-howto`, which was updated for Python 3.0. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 240 | |
Gregory P. Smith | f3655c4 | 2008-12-02 23:52:53 +0000 | [diff] [blame] | 241 | Views And Iterators Instead Of Lists |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 242 | ------------------------------------- |
| 243 | |
| 244 | Some well-known APIs no longer return lists: |
| 245 | |
| 246 | * :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and |
| 247 | :meth:`dict.values` return "views" instead of lists. For example, |
| 248 | this no longer works: ``k = d.keys(); k.sort()``. Use ``k = |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 249 | sorted(d)`` instead (this works in Python 2.5 too, and is just |
| 250 | as efficient). |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 251 | |
| 252 | * Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and |
| 253 | :meth:`dict.itervalues` methods are no longer supported. |
Guido van Rossum | 4a98a2a | 2008-11-21 18:35:43 +0000 | [diff] [blame] | 254 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 255 | * :func:`map` and :func:`filter` return iterators. A quick fix is e.g. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 256 | ``list(map(...))``, but a better fix is often to use a list |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 257 | comprehension (especially when the original code uses :keyword:`lambda`). |
| 258 | Particularly tricky is :func:`map` invoked for the side effects of the |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 259 | function; the correct transformation is to use a for-loop. |
| 260 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 261 | * :func:`range` now behaves like :func:`xrange` used to behave, except |
| 262 | it works with values of arbitrary size. The latter no longer |
| 263 | exists. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 264 | |
| 265 | * :func:`zip` now returns an iterator. |
| 266 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 267 | Ordering Comparisons |
| 268 | -------------------- |
| 269 | |
| 270 | Python 3.0 has simplified the rules for ordering comparisons: |
| 271 | |
| 272 | * The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``) |
| 273 | raise a TypeError exception when the operands don't have a |
| 274 | meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0 |
| 275 | > None`` or ``len <= len`` are no longer valid. A corollary is that |
| 276 | sorting a heterogeneous list no longer makes sense -- all the |
| 277 | elements must be comparable to each other. Note that this does not |
| 278 | apply to the ``==`` and ``!=`` operators: objects of different |
| 279 | uncomparable types always compare unequal to each other, and an |
| 280 | object always compares equal to itself (i.e., ``x is y`` implies ``x |
| 281 | = y``; this is true even for ``NaN``). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 282 | |
Kurt B. Kaiser | 2bc6b5e | 2008-02-13 18:03:11 +0000 | [diff] [blame] | 283 | * :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the *cmp* |
Kurt B. Kaiser | 9d0d616 | 2008-02-14 02:47:50 +0000 | [diff] [blame] | 284 | argument providing a comparison function. Use the *key* argument |
Kurt B. Kaiser | 2bc6b5e | 2008-02-13 18:03:11 +0000 | [diff] [blame] | 285 | instead. N.B. the *key* and *reverse* arguments are now "keyword-only". |
Kurt B. Kaiser | a140101 | 2008-02-13 16:09:27 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 287 | * The :func:`cmp` function is gone, and the :meth:`__cmp__` special |
| 288 | method is no longer supported. Use :meth:`__lt__` for sorting, |
| 289 | :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 290 | needed. (If you really need the :func:`cmp` functionality, you |
| 291 | could use the expression ``(a > b) - (a < b)`` as the equivalent for |
| 292 | ``cmp(a, b)``.) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 293 | |
| 294 | Integers |
| 295 | -------- |
| 296 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 297 | * :pep:`0237`: Essentially, :class:`long` renamed to :class:`int`. |
| 298 | That is, there is only one built-in integral type, named |
| 299 | :class:`int`; but it behaves mostly like the old :class:`long` type. |
Guido van Rossum | 4a98a2a | 2008-11-21 18:35:43 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 301 | * :pep:`0238`: An expression like ``1/2`` returns a float. Use |
| 302 | ``1//2`` to get the truncating behavior. (The latter syntax has |
| 303 | existed for years, at least since Python 2.2.) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 305 | * The :data:`sys.maxint` constant was removed, since there is no |
| 306 | longer a limit to the value of ints. However, :data:`sys.maxsize` |
| 307 | can be used as an integer larger than any practical list or string |
| 308 | index. It conforms to the implementation's "natural" integer size |
| 309 | and is typically the same as :data:`sys.maxint` in previous releases |
| 310 | on the same platform (assuming the same build options). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 312 | * The :func:`repr` of a long integer doesn't include the trailing ``L`` |
| 313 | anymore, so code that unconditionally strips that character will |
| 314 | chop off the last digit instead. (Use :func:`str` instead.) |
| 315 | |
| 316 | * Octal literals are no longer of the form ``0720``; use ``0o720`` |
| 317 | instead. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 318 | |
| 319 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 320 | Overview Of Syntax Changes |
| 321 | ========================== |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 323 | This section gives a brief overview of every *syntactic* change in |
| 324 | Python 3.0. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 326 | New Syntax |
| 327 | ---------- |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 329 | * :pep:`3107`: Function argument and return value annotations. This |
| 330 | provides a standardized way of annotating a function's parameters |
| 331 | and return value. There are no semantics attached to such |
| 332 | annotations except that they can be introspected at runtime using |
| 333 | the :attr:`__annotations__` attribute. The intent is to encourage |
| 334 | experimentation through metaclasses, decorators or frameworks. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 336 | * :pep:`3102`: Keyword-only arguments. Named parameters occurring |
| 337 | after ``*args`` in the parameter list *must* be specified using |
| 338 | keyword syntax in the call. You can also use a bare ``*`` in the |
| 339 | parameter list to indicate that you don't accept a variable-length |
| 340 | argument list, but you do have keyword-only arguments. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 341 | |
| 342 | * Keyword arguments are allowed after the list of base classes in a |
| 343 | class definition. This is used by the new convention for specifying |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 344 | a metaclass (see :pep:`3115`), but can be used for other purposes as |
| 345 | well, as long as the metaclass supports it. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 347 | * :pep:`3104`: :keyword:`nonlocal` statement. Using ``nonlocal x`` |
| 348 | you can now assign directly to a variable in an outer (but |
| 349 | non-global) scope. :keyword:`nonlocal` is a new reserved word. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 351 | * :pep:`3132`: Extended Iterable Unpacking. You can now write things |
| 352 | like ``a, b, *rest = some_sequence``. And even ``*rest, a = |
| 353 | stuff``. The ``rest`` object is always a (possibly empty) list; the |
| 354 | right-hand side may be any iterable. Example:: |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 356 | (a, *rest, b) = range(5) |
| 357 | |
| 358 | This sets *a* to ``0``, *b* to ``4``, and \*rest to ``[1, 2, 3]``. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 359 | |
| 360 | * Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 361 | same thing as ``dict(stuff)`` but is more flexible. (This is |
| 362 | :pep:`0274` vindicated. :-) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 363 | |
| 364 | * Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 365 | dictionary; use ``set()`` for an empty set. Set comprehensions are |
Guido van Rossum | 7396135 | 2008-12-03 00:54:52 +0000 | [diff] [blame] | 366 | also supported; e.g., ``{x for x in stuff}`` means the same thing as |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 367 | ``set(stuff)`` but is more flexible. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 368 | |
| 369 | * New octal literals, e.g. ``0o720`` (already in 2.6). The old octal |
Walter Dörwald | eab34c9 | 2008-12-02 11:58:09 +0000 | [diff] [blame] | 370 | literals (``0720``) are gone. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 371 | |
| 372 | * New binary literals, e.g. ``0b1010`` (already in 2.6). |
| 373 | |
| 374 | * Bytes literals are introduced with a leading ``b`` or ``B``. |
| 375 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 376 | Changed Syntax |
| 377 | -------------- |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 378 | |
| 379 | * New :keyword:`raise` statement syntax: ``raise [expr [from expr]]``. |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 380 | Also note that string exceptions are no longer legal (:pep:`0352`). |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 382 | * :keyword:`as` and :keyword:`with` are now reserved words. (Since |
| 383 | 2.6, actually.) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 384 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 385 | * :keyword:`True`, :keyword:`False`, and :keyword:`None` are reserved |
| 386 | words. (2.6 partially enforced the restrictions on :keyword:`None` |
| 387 | already.) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 389 | * Change from :keyword:`except` *exc*, *var* to |
| 390 | :keyword:`except` *exc* :keyword:`as` *var*. See :pep:`3110`. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 392 | * List comprehensions no longer support the syntactic form |
| 393 | ``[... for var in item1, item2, ...]``. Use |
| 394 | ``[... for var in (item1, item2, ...)]`` instead. |
| 395 | Also note that list comprehensions have different semantics: they |
| 396 | are closer to syntactic sugar for a generator expression inside a |
| 397 | :func:`list` constructor, and in particular the loop control |
| 398 | variables are no longer leaked into the surrounding scope. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 400 | * The *ellipsis* (``...``) can be used as an atomic expression |
| 401 | anywhere. (Previously it was only allowed in slices.) Also, it |
| 402 | *must* now be spelled as ``...``. (Previously it could also be |
| 403 | spelled as ``. . .``, by a mere accident of the grammar.) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 404 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 405 | Removed Syntax |
| 406 | -------------- |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 408 | * :pep:`3113`: Tuple parameter unpacking removed. You can no longer |
| 409 | write ``def foo(a, (b, c)): ...``. |
| 410 | Use ``def foo(a, b_c): b, c = b_c`` instead. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 412 | * Removed backticks (use :func:`repr` instead). |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 414 | * Removed ``<>`` (use ``!=`` instead). |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 415 | |
| 416 | * Removed keyword: :func:`exec` is no longer a keyword; it remains as |
| 417 | a function. (Fortunately the function syntax was also accepted in |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 418 | 2.x.) Also note that :func:`exec` no longer takes a stream argument; |
| 419 | instead of ``exec(f)`` you can use ``exec(f.read())``. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 420 | |
| 421 | * Integer literals no longer support a trailing ``l`` or ``L``. |
| 422 | |
| 423 | * String literals no longer support a leading ``u`` or ``U``. |
| 424 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 425 | * The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only |
| 426 | allowed at the module level, no longer inside functions. |
| 427 | |
| 428 | * The only acceptable syntax for relative imports is :keyword:`from` |
| 429 | ``.``[*module*] :keyword:`import` *name*; :keyword:`import` forms |
| 430 | not starting with ``.`` are always interpreted as absolute imports. |
| 431 | (:pep:`0328`) |
| 432 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 434 | Changes Already Present In Python 2.6 |
| 435 | ===================================== |
| 436 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 437 | Since many users presumably make the jump straight from Python 2.5 to |
| 438 | Python 3.0, this section reminds the reader of new features that were |
| 439 | originally designed for Python 3.0 but that were back-ported to Python |
| 440 | 2.6. The corresponding sections in :ref:`whats-new-in-2.6` should be |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 441 | consulted for longer descriptions. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 443 | * :ref:`pep-0343`. The :keyword:`with` statement is now a standard |
| 444 | feature and no longer needs to be imported from the ``__future__``. |
| 445 | Also check out :ref:`new-26-context-managers` and |
| 446 | :ref:`new-module-contextlib`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 448 | * :ref:`pep-0366`. This enhances the usefulness of the :option:`-m` |
| 449 | option when the referenced module lives in a package. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 451 | * :ref:`pep-0370`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 453 | * :ref:`pep-0371`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 455 | * :ref:`pep-3101`. Note: the 2.6 description mentions the |
| 456 | :meth:`format` method for both 8-bit and Unicode strings. In 3.0, |
| 457 | only the :class:`str` type (text strings with Unicode support) |
| 458 | supports this method; the :class:`bytes` type does not. The plan is |
| 459 | to eventually make this the only API for string formatting, and to |
| 460 | start deprecating the ``%`` operator in Python 3.1. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 462 | * :ref:`pep-3105`. This is now a standard feature and no longer needs |
Guido van Rossum | 67d75ba | 2008-12-03 02:31:31 +0000 | [diff] [blame^] | 463 | to be imported from :mod:`__future__`. More details were given above. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 464 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 465 | * :ref:`pep-3110`. The :keyword:`except` *exc* :keyword:`as` *var* |
| 466 | syntax is now standard and :keyword:`except` *exc*, *var* is no |
| 467 | longer supported. (Of course, the :keyword:`as` *var* part is still |
| 468 | optional.) |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 470 | * :ref:`pep-3112`. The ``b"..."`` string literal notation (and its |
| 471 | variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 472 | produces a literal of type :class:`bytes`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 474 | * :ref:`pep-3116`. The :mod:`io` module is now the standard way of |
| 475 | doing file I/O, and the initial values of :data:`sys.stdin`, |
| 476 | :data:`sys.stdout` and :data:`sys.stderr` are now instances of |
| 477 | :class:`io.TextIOBase`. The builtin :func:`open` function is now an |
| 478 | alias for :func:`io.open` and has additional keyword arguments |
| 479 | *encoding*, *errors*, *newline* and *closefd*. Also note that an |
| 480 | invalid *mode* argument now raises :exc:`ValueError`, not |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 481 | :exc:`IOError`. The binary file object underlying a text file |
| 482 | object can be accessed as :attr:`f.buffer` (but beware that the |
| 483 | text object maintains a buffer of itself in order to speed up |
| 484 | the encoding and decoding operations). |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 486 | * :ref:`pep-3118`. The old builtin :func:`buffer` is now really gone; |
| 487 | the new builtin :func:`memoryview` provides (mostly) similar |
| 488 | functionality. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 490 | * :ref:`pep-3119`. The :mod:`abc` module and the ABCs defined in the |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 491 | :mod:`collections` module plays a somewhat more prominent role in |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 492 | the language now, and builtin collection types like :class:`dict` |
| 493 | and :class:`list` conform to the :class:`collections.MutableMapping` |
| 494 | and :class:`collections.MutableSequence` ABC, respectively. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 495 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 496 | * :ref:`pep-3127`. As mentioned above, the new octal literal |
| 497 | notation is the only one supported, and binary literals have been |
| 498 | added. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 500 | * :ref:`pep-3129`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 502 | * :ref:`pep-3141`. The :mod:`numbers` module is another new use of |
| 503 | ABCs, defining Python's "numeric tower". Also note the new |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 504 | :mod:`fractions` module which implements :class:`numbers.Rational`. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 505 | |
| 506 | |
| 507 | Library Changes |
| 508 | =============== |
| 509 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 510 | Due to time constraints, this document does not exhaustively cover the |
| 511 | very extensive changes to the standard library. :pep:`3108` is the |
| 512 | reference for the major changes to the library. Here's a capsule |
| 513 | review: |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 515 | * Many old modules were removed. Some, like :mod:`gopherlib` (no |
| 516 | longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were |
| 517 | already deprecated by :pep:`0004`. Others were removed as a result |
| 518 | of the removal of support for various platforms such as Irix, BeOS |
| 519 | and Mac OS 9 (see :pep:`0011`). Some modules were also selected for |
| 520 | removal in Python 3.0 due to lack of use or because a better |
| 521 | replacement exists. See :pep:`3108` for an exhaustive list. |
Guido van Rossum | 5b78dd9 | 2008-12-02 17:31:14 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 523 | * The :mod:`bsddb3` package was removed because its presence in the |
| 524 | core standard library has proved over time to be a particular burden |
| 525 | for the core developers due to testing instability and Berlekey DB's |
| 526 | release schedule. However, the package is alive and well, |
| 527 | externally maintained at http://www.jcea.es/programacion/pybsddb.htm. |
| 528 | |
| 529 | * Some modules were renamed because their old name flaunted |
| 530 | :pep:`0008`, or for various other reasons: |
| 531 | |
| 532 | ======================= ======================= |
| 533 | Old Name New Name |
| 534 | ======================= ======================= |
| 535 | _winreg winreg |
| 536 | ConfigParser configparser |
| 537 | copy_reg copyreg |
| 538 | Queue queue |
| 539 | SocketServer socketserver |
| 540 | markupbase _markupbase |
| 541 | repr reprlib |
| 542 | test.test_support test.support |
| 543 | ======================= ======================= |
| 544 | |
| 545 | * A common pattern in Python 2.x is to have one version of a module |
| 546 | implemented in pure Python, with an optional accelerated version |
| 547 | implemented as a C extension; for example, :mod:`pickle` and |
| 548 | :mod:`cPickle`. This places the burden of importing the accelerated |
| 549 | version and falling back on the pure Python version on each user of |
| 550 | these modules. In Python 3.0, the accelerated versions are |
| 551 | considered implementation details of the pure Python versions. |
| 552 | Users should always import the standard version, which attempts to |
| 553 | import the accelerated version and falls back to the pure Python |
| 554 | version. The :mod:`pickle` module received this treatment. The |
| 555 | :mod:`profile` module is on the list for 3.1. The :mod:`StringIO` |
| 556 | module has been turned into a class in the :mod:`io` module. |
| 557 | |
| 558 | * Some related modules have been grouped into packages, and usually |
| 559 | the submodule names have been simplified. The resulting new |
| 560 | packages are: |
| 561 | |
| 562 | * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`, |
| 563 | :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`). |
| 564 | |
| 565 | * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`). |
| 566 | |
| 567 | * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`, |
| 568 | :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`, |
| 569 | :mod:`cookielib`). |
| 570 | |
| 571 | * :mod:`tkinter` (all :mod:`Tkinter`-related modules except |
| 572 | :mod:`turtle`). The target audience of :mod:`turtle` doesn't |
| 573 | really care about :mod:`tkinter`. Also note that as of Python |
| 574 | 2.6, the functionality of :mod:`turtle` has been greatly enhanced. |
| 575 | |
| 576 | * :mod:`urllib` (:mod:`urllib`, :mod:`urllib`2, :mod:`urlparse`, |
| 577 | :mod:`robotparse`). |
| 578 | |
| 579 | * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`, |
| 580 | :mod:`SimpleXMLRPCServer`). |
| 581 | |
| 582 | Some other library changes (not covered by :pep:`3108`): |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 583 | |
| 584 | * Killed :mod:`sets`. Use the builtin :func:`set` function. |
| 585 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 586 | * Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`, |
| 587 | :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`, |
| 588 | :data:`sys.exc_traceback`. (Note that :data:`sys.last_type` |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 589 | etc. remain.) |
| 590 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 591 | * Cleanup of the :class:`array.array` type: the :meth:`read` and |
| 592 | :meth:`write` methods are gone; use :meth:`fromfile` and |
| 593 | :meth:`tofile` instead. |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 595 | * Cleanup of the :mod:`operator` module: removed |
| 596 | :func:`sequenceIncludes` and :func:`isCallable`. |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 598 | * Cleanup of the :mod:`thread` module: :func:`acquire_lock` and |
| 599 | :func:`release_lock` are gone; use :func:`acquire` and |
| 600 | :func:`release` instead. |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | c46ee54 | 2008-12-02 23:46:46 +0000 | [diff] [blame] | 602 | * Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API. |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 605 | :pep:`3101`: A New Approach To String Formatting |
| 606 | ================================================ |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 607 | |
Guido van Rossum | 4a98a2a | 2008-11-21 18:35:43 +0000 | [diff] [blame] | 608 | * A new system for built-in string formatting operations replaces the |
| 609 | ``%`` string formatting operator. (However, the ``%`` operator is |
| 610 | still supported; it will be deprecated in Python 3.1 and removed |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 611 | from the language at some later time.) Read :pep:`3101` for the full |
| 612 | scoop. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 613 | |
| 614 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 615 | :pep:`3106`: Revamping dict :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values` |
| 616 | ========================================================================================= |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 617 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 618 | .. XXX expand this (but note that the "pitfalls" section currently has |
| 619 | .. XXX more detail :-) |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 620 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 621 | * The :meth:`dict.iterkeys`, :meth:`dict.itervalues` and :meth:`dict.iteritems` |
| 622 | methods have been removed. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 623 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 624 | * :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` return objects |
Guido van Rossum | 4a98a2a | 2008-11-21 18:35:43 +0000 | [diff] [blame] | 625 | with set behavior that reference the underlying dict; these are often |
| 626 | referred to as *dictionary views*. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 627 | |
| 628 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 629 | Exception Stuff |
| 630 | =============== |
| 631 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 632 | * :pep:`0352`: All exceptions must be derived (directly or indirectly) |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 633 | from :exc:`BaseException`. This is the root of the exception |
| 634 | hierarchy. Most exceptions should actually be derived from |
| 635 | :exc:`Exception`. This is not a new recommendation, but the |
| 636 | *requirement* to inherit from :exc:`BaseException` is new. (Python |
| 637 | 2.6 still allowed classic classes to be raised, and placed no |
| 638 | restriction on what you can catch.) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 639 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 640 | * :exc:`StandardError` was removed (in 2.6, actually). |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 641 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 642 | * Dropping sequence behavior (slicing!) and :attr:`message` attribute of |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 643 | exception instances. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 645 | * :pep:`3109`: Raising exceptions. You must now use ``raise Exception(args)`` |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 646 | instead of ``raise Exception, args``. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 648 | * :pep:`3110`: Catching exceptions. You must now use ``except SomeException as |
Georg Brandl | e06de8b | 2008-05-05 21:42:51 +0000 | [diff] [blame] | 649 | identifier:`` instead of ``except Exception, identifier:`` |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 651 | * :pep:`3134`: Exception chaining. XXX |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 652 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 653 | * A few exception messages are improved when Windows fails to load an extension |
| 654 | module. For example, ``error code 193`` is now ``%1 is not a valid Win32 |
| 655 | application``. Strings now deal with non-English locales. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 656 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 657 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 658 | New Class And Metaclass Stuff |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 659 | ============================= |
| 660 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 661 | XXX Move to new syntax section??? |
| 662 | |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 663 | * Classic classes are gone. |
| 664 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 665 | * :pep:`3115`: New Metaclass Syntax. Instead of:: |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 666 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 667 | class C: |
| 668 | __metaclass__ = M |
| 669 | ... |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 671 | you now use:: |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 672 | |
Guido van Rossum | 3828768 | 2008-12-03 02:03:19 +0000 | [diff] [blame] | 673 | class C(metaclass=M): |
| 674 | ... |
| 675 | |
| 676 | The module-global :data:`__metaclass__` variable is no longer supported. |
| 677 | (It was a crutch to make it easier to default to new-style classes |
| 678 | without deriving every class from :class:`object`.) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 679 | |
| 680 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 681 | Other Language Changes |
| 682 | ====================== |
| 683 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 684 | * Moved :func:`intern` to :func:`sys.intern`. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 685 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 686 | * ``!=`` now returns the opposite of ``==``, unless ``==`` returns |
| 687 | ``NotImplemented``. |
| 688 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 689 | * The concept of "unbound methods" was removed from the language. |
| 690 | When referencing a method as a class attribute, you now get a plain |
| 691 | function object. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 693 | * :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__` |
| 694 | were killed. The syntax ``a[i:j]`` now translates to |
| 695 | ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or |
| 696 | :meth:`__delitem__`, when used as an assignment or deletion target, |
| 697 | respectively). |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 699 | * :pep:`3111`: :func:`raw_input` renamed to :func:`input`. That is, |
| 700 | the new :func:`input` function reads a line from :data:`sys.stdin` |
| 701 | and returns it with the trailing newline stripped. It raises |
| 702 | :exc:`EOFError` if the input is terminated prematurely. To get the |
| 703 | old behavior of :func:`input`, use ``eval(input())``. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 704 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 705 | * :pep:`3114`: ``.next()`` renamed to :meth:`__next__`, new builtin |
| 706 | :func:`next` to call the :meth:`__next__` method on an object. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 707 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 708 | * :pep:`3135`: New :func:`super`. You can now invoke :func:`super` |
| 709 | without arguments and the right class and instance will |
| 710 | automatically be chosen. With arguments, its behavior is unchanged. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 711 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 712 | * :func:`zip`, :func:`map` and :func:`filter` return iterators. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 713 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 714 | * :data:`string.letters` and its friends (:data:`string.lowercase` and |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 715 | :data:`string.uppercase`) are gone. Use |
| 716 | :data:`string.ascii_letters` etc. instead. (The reason for the |
| 717 | removal is that :data:string.letters` and friends had |
| 718 | locale-specific behavior, which is a bad idea for such |
| 719 | attractively-named global "constants".) |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 720 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 721 | * Removed: :func:`apply`. Instead of ``apply(f, args)`` use |
| 722 | ``f(*args)``. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 724 | * Removed :func:`callable`. Instead of ``callable(f)`` you can use |
| 725 | ``hasattr(f, '__call__')``. The :func:`operator.isCallable` function |
| 726 | is also gone. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 728 | * Removed :func:`coerce`. This function no longer serves a purpose |
| 729 | now that classic classes are gone. |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 730 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 731 | * Removed :func:`execfile`. Instead of ``execfile(fn)`` use |
| 732 | ``exec(open(fn).read())``. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 734 | * Removed :class:`file`. Use :func:`open`. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 735 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 736 | * Removed :func:`reduce`. Use :func:`functools.reduce` if you really |
| 737 | need it; however, 99 percent of the time an explicit :keyword:`for` |
| 738 | loop is more readable. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 739 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 740 | * Removed :func:`reload`. Use :func:`imp.reload`. |
| 741 | |
| 742 | * Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator |
| 743 | instead. |
| 744 | |
| 745 | * The :meth:`__oct__` and :meth:`__hex__` special methods are removed |
| 746 | -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert |
| 747 | the argument to an integer. |
| 748 | |
| 749 | * Removed support for :attr:`__members__` and :attr:`__methods__`. |
| 750 | |
| 751 | * Renamed the boolean conversion C-level slot and method: |
| 752 | ``nb_nonzero`` is now ``nb_bool`` and :meth:`__nonzero__` is now |
| 753 | :meth:`__bool__`. |
| 754 | |
| 755 | * Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the |
| 756 | underscores, adding an 's'). The :data:`__builtins__` variable |
| 757 | found in most global namespaces is unchanged. To modify a builtin, |
| 758 | you should use :mod:`builtins`, not :data:`__builtins__`! |
| 759 | |
| 760 | * Renamed function attributes :attr:`func_whatever` to |
| 761 | :attr:`__whatever__`. XXX list every single one. |
| 762 | |
| 763 | * Removed :exc:`StandardError`. |
| 764 | |
| 765 | * Removed METH_OLDARGS and WITH_CYCLE_GC. XXX more. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 766 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 767 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 768 | |
| 769 | |
| 770 | Optimizations |
| 771 | ------------- |
| 772 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 773 | The net result of the 3.0 generalizations is that Python 3.0 runs the |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 774 | pystone benchmark around 10% slower than Python 2.5. Most likely the |
| 775 | biggest cause is the removal of special-casing for small integers. |
| 776 | There's room for improvement, but it will happen after 3.0 is |
| 777 | released! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 778 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 779 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 780 | |
| 781 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 782 | New, Improved, And Deprecated Modules |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 783 | ===================================== |
| 784 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 785 | As usual, Python's standard library received a number of enhancements and bug |
| 786 | fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| 787 | by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| 788 | complete list of changes, or look through the Subversion logs for all the |
| 789 | details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 790 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 791 | * The :mod:`cPickle` module is gone. Use :mod:`pickle` instead. Eventually |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 792 | we'll have a transparent accelerator module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 793 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 794 | * The :mod:`imageop` module is gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 795 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 796 | * The :mod:`audiodev`, :mod:`Bastion`, :mod:`bsddb185`, :mod:`exceptions`, |
| 797 | :mod:`linuxaudiodev`, :mod:`md5`, :mod:`MimeWriter`, :mod:`mimify`, |
| 798 | :mod:`popen2`, :mod:`rexec`, :mod:`sets`, :mod:`sha`, :mod:`stringold`, |
| 799 | :mod:`strop`, :mod:`sunaudiodev`, :mod:`timing`, and :mod:`xmllib` modules are |
| 800 | gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 801 | |
Gregory P. Smith | 7b9a222 | 2008-09-04 05:07:03 +0000 | [diff] [blame] | 802 | * The :mod:`bsddb` module is gone. It is being maintained externally |
| 803 | with its own release schedule better mirroring that of BerkeleyDB. |
| 804 | See http://www.jcea.es/programacion/pybsddb.htm. |
| 805 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 806 | * The :mod:`new` module is gone. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 807 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 808 | * The functions :func:`os.tmpnam`, :func:`os.tempnam` and :func:`os.tmpfile` |
| 809 | have been removed in favor of the :mod:`tempfile` module. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 810 | |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 811 | * The :mod:`tokenize` module has been changed to work with bytes. The main |
| 812 | entry point is now :func:`tokenize.tokenize`, instead of generate_tokens. |
| 813 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 814 | .. ====================================================================== |
| 815 | .. whole new modules get described in subsections here |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 816 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 817 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 818 | |
| 819 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 820 | Build And C API Changes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 821 | ======================= |
| 822 | |
| 823 | Changes to Python's build process and to the C API include: |
| 824 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 825 | * :pep:`3118`: New Buffer API. XXX |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 827 | * :pep:`3121`: Extension Module Initialization & Finalization. XXX |
Guido van Rossum | b197f3c | 2007-08-31 00:37:00 +0000 | [diff] [blame] | 828 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 829 | * :pep:`3123`: Making :cmacro:`PyObject_HEAD` conform to standard C. XXX |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 830 | |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 831 | * No more C API support for restricted execution. |
| 832 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 833 | * :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`, :cfunc:`PyMember_Get`, |
| 834 | and :cfunc:`PyMember_Set` C APIs are removed. |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 835 | |
Georg Brandl | ec17d20 | 2008-02-02 10:44:37 +0000 | [diff] [blame] | 836 | * New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like |
| 837 | :cfunc:`PyImport_ImportModule` but won't block on the import lock (returning |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 838 | an error instead). |
| 839 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 840 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 841 | |
| 842 | |
| 843 | Port-Specific Changes |
| 844 | --------------------- |
| 845 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 846 | XXX Platform-specific changes go here. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | 715287f | 2008-12-02 22:34:15 +0000 | [diff] [blame] | 848 | * XXX BeOS, RISCOS, Irix, Tru64 support |
Georg Brandl | 396ef80 | 2008-02-02 10:30:18 +0000 | [diff] [blame] | 849 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 850 | .. ====================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 851 | |
| 852 | |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 853 | Porting To Python 3.0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 854 | ===================== |
| 855 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 856 | For porting existing Python 2.5 or 2.6 source code to Python 3.0, the |
| 857 | best strategy is the following: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 858 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 859 | 0. (Prerequisite:) Start with excellent test coverage. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 860 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 861 | 1. Port to Python 2.6. This should be no more work than the average |
| 862 | port from Python 2.x to Python 2.(x+1). Make sure all your tests |
| 863 | pass. |
Christian Heimes | f78b1c6 | 2007-12-02 16:52:32 +0000 | [diff] [blame] | 864 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 865 | 2. (Still using 2.6:) Turn on the :option:`-3` command line switch. |
| 866 | This enables warnings about features that will be removed (or |
| 867 | change) in 3.0. Run your test suite again, and fix code that you |
| 868 | get warnings about until there are no warnings left, and all your |
| 869 | tests still pass. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 870 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 871 | 3. Run the ``2to3`` source-to-source translator over your source code |
| 872 | tree. (See :ref:`2to3-reference` for more on this tool.) Run the |
| 873 | result of the translation under Python 3.0. Manually fix up any |
| 874 | remaining issues, fixing problems until all tests pass again. |
| 875 | |
| 876 | It is not recommended to try to write source code that runs unchanged |
| 877 | under both Python 2.6 and 3.0; you'd have to use a very contorted |
| 878 | coding style, e.g. avoiding :keyword:`print` statements, metaclasses, |
| 879 | and much more. If you are maintaining a library that needs to support |
| 880 | both Python 2.6 and Python 3.0, the best approach is to modify step 3 |
| 881 | above by editing the 2.6 version of the source code and running the |
| 882 | ``2to3`` translator again, rather than editing the 3.0 version of the |
| 883 | source code. |
| 884 | |
| 885 | For porting C extensions to Python 3.0, please see :ref:`cporting-howto`. |
Guido van Rossum | eb3d8d4 | 2008-12-02 00:56:25 +0000 | [diff] [blame] | 886 | |
Georg Brandl | 5a16558 | 2007-08-31 06:15:01 +0000 | [diff] [blame] | 887 | .. ====================================================================== |