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