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