blob: 9592d80a26b42bdfb540609716be2c28e73e7f0e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
Guido van Rossumeb3d8d42008-12-02 00:56:25 +00002 What's New In Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +00003****************************
4
Guido van Rossum715287f2008-12-02 22:34:15 +00005.. XXX Add trademark info for Apple, Microsoft.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Guido van Rossum715287f2008-12-02 22:34:15 +00007.. XXX Remove duplicates; just put info in the most relevant section.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +00008
Guido van Rossum4a98a2a2008-11-21 18:35:43 +00009:Author: Guido van Rossum
10:Release: |release|
11:Date: |today|
12
13.. $Id$
14 Rules for maintenance:
Georg Brandl5a165582007-08-31 06:15:01 +000015
16 * Anyone can add text to this document. Do not spend very much time
17 on the wording of your changes, because your text will probably
18 get rewritten to some degree.
19
20 * The maintainer will go through Misc/NEWS periodically and add
21 changes; it's therefore more important to add your changes to
Guido van Rossum67d75ba2008-12-03 02:31:31 +000022 Misc/NEWS than to this file. (Note: I didn't get to this for 3.0.
23 GvR.)
Georg Brandl5a165582007-08-31 06:15:01 +000024
25 * This is not a complete list of every single change; completeness
26 is the purpose of Misc/NEWS. Some changes I consider too small
27 or esoteric to include. If such a change is added to the text,
28 I'll just remove it. (This is another reason you shouldn't spend
29 too much time on writing your addition.)
30
31 * If you want to draw your new text to the attention of the
32 maintainer, add 'XXX' to the beginning of the paragraph or
33 section.
34
35 * It's OK to just add a fragmentary note about a change. For
36 example: "XXX Describe the transmogrify() function added to the
37 socket module." The maintainer will research the change and
38 write the necessary text.
39
40 * You can comment out your additions if you like, but it's not
41 necessary (especially when a final release is some months away).
42
43 * Credit the author of a patch or bugfix. Just the name is
Guido van Rossum67d75ba2008-12-03 02:31:31 +000044 sufficient; the e-mail address isn't necessary. (Due to time
45 constraints I haven't managed to do this for 3.0. GvR.)
Georg Brandl5a165582007-08-31 06:15:01 +000046
47 * It's helpful to add the bug/patch number as a comment:
48
49 % Patch 12345
50 XXX Describe the transmogrify() function added to the socket
51 module.
52 (Contributed by P.Y. Developer.)
53
54 This saves the maintainer the effort of going through the SVN log
Guido van Rossum67d75ba2008-12-03 02:31:31 +000055 when researching a change. (Again, I didn't get to this for 3.0.
56 GvR.)
Georg Brandl116aa622007-08-15 14:28:22 +000057
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000058This article explains the new features in Python 3.0, compared to 2.6.
Guido van Rossumc46ee542008-12-02 23:46:46 +000059Python 3.0, also known as "Python 3000" or "Py3K", is the first ever
Guido van Rossum715287f2008-12-02 22:34:15 +000060*intentionally incompatible* release. There are more changes than in
61a typical release, and more that are important for all Python users.
62Nevertheless, after digesting the changes, you'll find that Python
63really hasn't changed all that much -- by and large, we're merely
64fixing well-known annoyances and warts.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000065
66This article doesn't attempt to provide a complete specification of
67the new features, but instead provides a convenient overview. For
68full details, you should refer to the documentation for Python 3.0. If
69you want to understand the complete implementation and design
70rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Georg Brandl5a165582007-08-31 06:15:01 +000072.. Compare with previous release in 2 - 3 sentences here.
73.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandl5a165582007-08-31 06:15:01 +000075.. ======================================================================
76.. Large, PEP-level features and changes should be described here.
77.. Should there be a new section here for 3k migration?
78.. Or perhaps a more general section describing module changes/deprecation?
79.. sets module deprecated
80.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Guido van Rossumb197f3c2007-08-31 00:37:00 +000083Common Stumbling Blocks
84=======================
85
Guido van Rossum715287f2008-12-02 22:34:15 +000086This section lists those few changes that are most likely to trip you
87up if you're used to Python 2.5.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000088
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000089Print Is A Function
90-------------------
Guido van Rossumdff1c312007-09-06 14:46:41 +000091
Guido van Rossum715287f2008-12-02 22:34:15 +000092The :keyword:`print` statement has been replaced with a :func:`print`
93function, with keyword arguments to replace most of the special syntax
94of the old :keyword:`print` statement (:pep:`3105`). Examples::
Guido van Rossumdff1c312007-09-06 14:46:41 +000095
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000096 Old: print "The answer is", 2*2
97 New: print("The answer is", 2*2)
Guido van Rossumdff1c312007-09-06 14:46:41 +000098
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000099 Old: print x, # Trailing comma suppresses newline
100 New: print(x, end=" ") # Appends a space instead of a newline
Guido van Rossumdff1c312007-09-06 14:46:41 +0000101
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000102 Old: print # Prints a newline
103 New: print() # You must call the function!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000104
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000105 Old: print >>sys.stderr, "fatal error"
106 New: print("fatal error", file=sys.stderr)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000107
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000108 Old: print (x, y) # prints repr((x, y))
109 New: print((x, y)) # Not the same as print(x, y)!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000110
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000111You can also customize the separator between items, e.g.::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000112
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000113 print("There are <", 2**32, "> possibilities!", sep="")
Guido van Rossumdff1c312007-09-06 14:46:41 +0000114
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000115which produces::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000116
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000117 There are <4294967296> possibilities!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000118
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000119Note:
Guido van Rossumdff1c312007-09-06 14:46:41 +0000120
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000121* The :func:`print` function doesn't support the "softspace" feature of
Guido van Rossum715287f2008-12-02 22:34:15 +0000122 the old :keyword:`print` statement. For example, in Python 2.x,
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000123 ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
124 ``print("A\n", "B")`` writes ``"A\n B\n"``.
Guido van Rossumdff1c312007-09-06 14:46:41 +0000125
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000126* Initially, you'll be finding yourself typing the old ``print x``
127 a lot in interactive mode. Time to retrain your fingers to type
128 ``print(x)`` instead!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000129
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000130* When using the ``2to3`` source-to-source conversion tool, all
Guido van Rossum715287f2008-12-02 22:34:15 +0000131 :keyword:`print` statements are automatically converted to
132 :func:`print` function calls, so this is mostly a non-issue for
133 larger projects.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000134
Guido van Rossum38287682008-12-03 02:03:19 +0000135Text Vs. Data Instead Of Unicode Vs. 8-bit
136------------------------------------------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000137
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000138Everything you thought you knew about binary data and Unicode has
Guido van Rossum38287682008-12-03 02:03:19 +0000139changed:
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000140
Guido van Rossum38287682008-12-03 02:03:19 +0000141XXX HIRO
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000142
Guido van Rossum38287682008-12-03 02:03:19 +0000143* Python 3.0 uses the concepts of *text* and (binary) *data* instead
144 of Unicode strings and 8-bit strings. All text is Unicode; however
145 *encoded* Unicode is represented as binary data. The type used to
146 hold text is :class:`str`, the type used to hold data is
147 :class:`bytes`. The difference is that any attempt to mix text and
148 data in Python 3.0 raises a TypeError exception, whereas if you were
149 to mix Unicode and 8-bit strings in Python 2.x, you would only get
150 an exception if the 8-bit string contained non-ASCII values. As a
151 consequence, pretty much all code that uses Unicode, encodings or
152 binary data most likely has to change. The change is for the
153 better, as in the 2.x world there were numerous bugs having to do
154 with mixing encoded and unencoded text.
155
156* You no longer use ``u"..."`` literals for Unicode text. However,
157 you must use ``b"..."`` literals for binary data.
Guido van Rossum73961352008-12-03 00:54:52 +0000158
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000159* Files opened as text files (still the default mode for :func:`open`)
160 always use an encoding to map between strings (in memory) and bytes
161 (on disk). Binary files (opened with a ``b`` in the mode argument)
162 always use bytes in memory. This means that if a file is opened
Guido van Rossum67d75ba2008-12-03 02:31:31 +0000163 using an incorrect mode or encoding, I/O will likely fail. It also
164 means that even Unix users will have to specify the correct mode
165 (text or binary) when opening a file. There is a platform-dependent
166 default encoding, which on Unixy platforms can be set with the
167 ``LANG`` environment variable (and sometimes also with some other
168 platform-specific locale-related environment variables). In many
169 cases, but not all, the system default is UTF-8; you should never
170 count on this default. Any application reading or writing more than
171 pure ASCII text should probably have a way to override the encoding.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000172
Guido van Rossum715287f2008-12-02 22:34:15 +0000173* The builtin :class:`basestring` abstract type was removed. Use
174 :class:`str` instead. The :class:`str` and :class:`bytes` types
175 don't have functionality enough in common to warrant a shared base
176 class.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000177
Guido van Rossum67d75ba2008-12-03 02:31:31 +0000178* Filenames are passed to and returned from APIs as (Unicode) strings.
179 This can present platform-specific problems because on some
180 platforms filenames are arbitrary byte strings. (On the other hand
181 on Windows, filenames are natively stored as Unicode.) As a
182 work-around, most APIs (e.g. :func:`open` and many functions in the
183 :mod:`os` module) that take filenames accept :class:`bytes` objects
184 as well as strings, and a few APIs have a way to ask for a
185 :class:`bytes` return value: :func:`os.listdir` returns a
186 :class:`bytes` instance if the argument is a :class:`bytes`
187 instance, and :func:`os.getcwdu` returns the current working
188 directory as a :class:`bytes` instance.
189
190* Some system APIs like :data:`os.environ` and :data:`sys.argv` can
191 also present problems when the bytes made available by the system is
192 not interpretable using the default encoding. Setting the ``LANG``
193 variable and rerunning the program is probably the best approach.
194
Guido van Rossum38287682008-12-03 02:03:19 +0000195* All backslashes in raw strings are interpreted literally. This
196 means that ``'\U'`` and ``'\u'`` escapes in raw strings are not
197 treated specially.
198
199XXX Deal with dupes below
200
201* There is only one text string type; its name is :class:`str` but its
202 behavior and implementation are like :class:`unicode` in 2.x.
203
204* The :class:`basestring` superclass has been removed. The ``2to3``
205 tool (see below) replaces every occurrence of :class:`basestring`
206 with :class:`str`.
207
208* :pep:`3137`: There is a new type, :class:`bytes`, to represent
209 binary data (and encoded text, which is treated as binary data until
210 it is decoded). The :class:`str` and :class:`bytes` types cannot be
211 mixed; you must always explicitly convert between them, using the
212 :meth:`str.encode` (str -> bytes) or :meth:`bytes.decode` (bytes ->
213 str) methods.
214
215* Like :class:`str`, the :class:`bytes` type is immutable. There is a
216 separate *mutable* type to hold buffered binary data,
217 :class:`bytearray`. Nearly all APIs that accept :class:`bytes` also
218 accept :class:`bytearray`. The mutable API is based on
219 :class:`collections.MutableSequence`.
220
221* :pep:`3138`: The :func:`repr` of a string no longer escapes
222 non-ASCII characters. It still escapes control characters and code
223 points with non-printable status in the Unicode standard, however.
224
225* :pep:`3120`: The default source encoding is now UTF-8.
226
227* :pep:`3131`: Non-ASCII letters are now allowed in identifiers.
228 (However, the standard library remains ASCII-only with the exception
229 of contributor names in comments.)
230
231* :pep:`3116`: New I/O implementation. The API is nearly 100%
232 backwards compatible, but completely reimplemented (currently largely
233 in Python). Also, binary files use bytes instead of strings.
234
235* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead,
236 import :class:`io.StringIO` or :class:`io.BytesIO`, for text and
237 data respectively.
238
Guido van Rossum715287f2008-12-02 22:34:15 +0000239* See also the :ref:`unicode-howto`, which was updated for Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000240
Gregory P. Smithf3655c42008-12-02 23:52:53 +0000241Views And Iterators Instead Of Lists
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000242-------------------------------------
243
244Some well-known APIs no longer return lists:
245
246* :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and
247 :meth:`dict.values` return "views" instead of lists. For example,
248 this no longer works: ``k = d.keys(); k.sort()``. Use ``k =
Guido van Rossum73961352008-12-03 00:54:52 +0000249 sorted(d)`` instead (this works in Python 2.5 too, and is just
250 as efficient).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000251
252* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
253 :meth:`dict.itervalues` methods are no longer supported.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000254
Georg Brandlec17d202008-02-02 10:44:37 +0000255* :func:`map` and :func:`filter` return iterators. A quick fix is e.g.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000256 ``list(map(...))``, but a better fix is often to use a list
Georg Brandlec17d202008-02-02 10:44:37 +0000257 comprehension (especially when the original code uses :keyword:`lambda`).
258 Particularly tricky is :func:`map` invoked for the side effects of the
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000259 function; the correct transformation is to use a for-loop.
260
Guido van Rossum73961352008-12-03 00:54:52 +0000261* :func:`range` now behaves like :func:`xrange` used to behave, except
262 it works with values of arbitrary size. The latter no longer
263 exists.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000264
265* :func:`zip` now returns an iterator.
266
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000267Ordering Comparisons
268--------------------
269
270Python 3.0 has simplified the rules for ordering comparisons:
271
272* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
273 raise a TypeError exception when the operands don't have a
274 meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0
275 > None`` or ``len <= len`` are no longer valid. A corollary is that
276 sorting a heterogeneous list no longer makes sense -- all the
277 elements must be comparable to each other. Note that this does not
278 apply to the ``==`` and ``!=`` operators: objects of different
279 uncomparable types always compare unequal to each other, and an
280 object always compares equal to itself (i.e., ``x is y`` implies ``x
281 = y``; this is true even for ``NaN``).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000282
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000283* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the *cmp*
Kurt B. Kaiser9d0d6162008-02-14 02:47:50 +0000284 argument providing a comparison function. Use the *key* argument
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000285 instead. N.B. the *key* and *reverse* arguments are now "keyword-only".
Kurt B. Kaisera1401012008-02-13 16:09:27 +0000286
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000287* The :func:`cmp` function is gone, and the :meth:`__cmp__` special
288 method is no longer supported. Use :meth:`__lt__` for sorting,
289 :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as
Guido van Rossum73961352008-12-03 00:54:52 +0000290 needed. (If you really need the :func:`cmp` functionality, you
291 could use the expression ``(a > b) - (a < b)`` as the equivalent for
292 ``cmp(a, b)``.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000293
294Integers
295--------
296
Guido van Rossum73961352008-12-03 00:54:52 +0000297* :pep:`0237`: Essentially, :class:`long` renamed to :class:`int`.
298 That is, there is only one built-in integral type, named
299 :class:`int`; but it behaves mostly like the old :class:`long` type.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000300
Guido van Rossum73961352008-12-03 00:54:52 +0000301* :pep:`0238`: An expression like ``1/2`` returns a float. Use
302 ``1//2`` to get the truncating behavior. (The latter syntax has
303 existed for years, at least since Python 2.2.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000304
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000305* The :data:`sys.maxint` constant was removed, since there is no
306 longer a limit to the value of ints. However, :data:`sys.maxsize`
307 can be used as an integer larger than any practical list or string
308 index. It conforms to the implementation's "natural" integer size
309 and is typically the same as :data:`sys.maxint` in previous releases
310 on the same platform (assuming the same build options).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000311
Guido van Rossum73961352008-12-03 00:54:52 +0000312* The :func:`repr` of a long integer doesn't include the trailing ``L``
313 anymore, so code that unconditionally strips that character will
314 chop off the last digit instead. (Use :func:`str` instead.)
315
316* Octal literals are no longer of the form ``0720``; use ``0o720``
317 instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000318
319
Guido van Rossum73961352008-12-03 00:54:52 +0000320Overview Of Syntax Changes
321==========================
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000322
Guido van Rossum73961352008-12-03 00:54:52 +0000323This section gives a brief overview of every *syntactic* change in
324Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000325
Guido van Rossum38287682008-12-03 02:03:19 +0000326New Syntax
327----------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000328
Guido van Rossum73961352008-12-03 00:54:52 +0000329* :pep:`3107`: Function argument and return value annotations. This
330 provides a standardized way of annotating a function's parameters
331 and return value. There are no semantics attached to such
332 annotations except that they can be introspected at runtime using
333 the :attr:`__annotations__` attribute. The intent is to encourage
334 experimentation through metaclasses, decorators or frameworks.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000335
Guido van Rossum715287f2008-12-02 22:34:15 +0000336* :pep:`3102`: Keyword-only arguments. Named parameters occurring
337 after ``*args`` in the parameter list *must* be specified using
338 keyword syntax in the call. You can also use a bare ``*`` in the
339 parameter list to indicate that you don't accept a variable-length
340 argument list, but you do have keyword-only arguments.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000341
342* Keyword arguments are allowed after the list of base classes in a
343 class definition. This is used by the new convention for specifying
Guido van Rossum73961352008-12-03 00:54:52 +0000344 a metaclass (see :pep:`3115`), but can be used for other purposes as
345 well, as long as the metaclass supports it.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000346
Guido van Rossum715287f2008-12-02 22:34:15 +0000347* :pep:`3104`: :keyword:`nonlocal` statement. Using ``nonlocal x``
348 you can now assign directly to a variable in an outer (but
349 non-global) scope. :keyword:`nonlocal` is a new reserved word.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000350
Guido van Rossum715287f2008-12-02 22:34:15 +0000351* :pep:`3132`: Extended Iterable Unpacking. You can now write things
352 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
353 stuff``. The ``rest`` object is always a (possibly empty) list; the
354 right-hand side may be any iterable. Example::
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000355
Guido van Rossum715287f2008-12-02 22:34:15 +0000356 (a, *rest, b) = range(5)
357
358 This sets *a* to ``0``, *b* to ``4``, and \*rest to ``[1, 2, 3]``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000359
360* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
Guido van Rossum73961352008-12-03 00:54:52 +0000361 same thing as ``dict(stuff)`` but is more flexible. (This is
362 :pep:`0274` vindicated. :-)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000363
364* Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty
Guido van Rossum715287f2008-12-02 22:34:15 +0000365 dictionary; use ``set()`` for an empty set. Set comprehensions are
Guido van Rossum73961352008-12-03 00:54:52 +0000366 also supported; e.g., ``{x for x in stuff}`` means the same thing as
Guido van Rossum715287f2008-12-02 22:34:15 +0000367 ``set(stuff)`` but is more flexible.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000368
369* New octal literals, e.g. ``0o720`` (already in 2.6). The old octal
Walter Dörwaldeab34c92008-12-02 11:58:09 +0000370 literals (``0720``) are gone.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000371
372* New binary literals, e.g. ``0b1010`` (already in 2.6).
373
374* Bytes literals are introduced with a leading ``b`` or ``B``.
375
Guido van Rossum38287682008-12-03 02:03:19 +0000376Changed Syntax
377--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000378
379* New :keyword:`raise` statement syntax: ``raise [expr [from expr]]``.
Guido van Rossum715287f2008-12-02 22:34:15 +0000380 Also note that string exceptions are no longer legal (:pep:`0352`).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000381
Guido van Rossum715287f2008-12-02 22:34:15 +0000382* :keyword:`as` and :keyword:`with` are now reserved words. (Since
383 2.6, actually.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000384
Guido van Rossum715287f2008-12-02 22:34:15 +0000385* :keyword:`True`, :keyword:`False`, and :keyword:`None` are reserved
386 words. (2.6 partially enforced the restrictions on :keyword:`None`
387 already.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000388
Guido van Rossum715287f2008-12-02 22:34:15 +0000389* Change from :keyword:`except` *exc*, *var* to
390 :keyword:`except` *exc* :keyword:`as` *var*. See :pep:`3110`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000391
Guido van Rossum715287f2008-12-02 22:34:15 +0000392* List comprehensions no longer support the syntactic form
393 ``[... for var in item1, item2, ...]``. Use
394 ``[... for var in (item1, item2, ...)]`` instead.
395 Also note that list comprehensions have different semantics: they
396 are closer to syntactic sugar for a generator expression inside a
397 :func:`list` constructor, and in particular the loop control
398 variables are no longer leaked into the surrounding scope.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000399
Guido van Rossum715287f2008-12-02 22:34:15 +0000400* The *ellipsis* (``...``) can be used as an atomic expression
401 anywhere. (Previously it was only allowed in slices.) Also, it
402 *must* now be spelled as ``...``. (Previously it could also be
403 spelled as ``. . .``, by a mere accident of the grammar.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000404
Guido van Rossum38287682008-12-03 02:03:19 +0000405Removed Syntax
406--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000407
Guido van Rossum715287f2008-12-02 22:34:15 +0000408* :pep:`3113`: Tuple parameter unpacking removed. You can no longer
409 write ``def foo(a, (b, c)): ...``.
410 Use ``def foo(a, b_c): b, c = b_c`` instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000411
Guido van Rossum715287f2008-12-02 22:34:15 +0000412* Removed backticks (use :func:`repr` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000413
Guido van Rossum715287f2008-12-02 22:34:15 +0000414* Removed ``<>`` (use ``!=`` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000415
416* Removed keyword: :func:`exec` is no longer a keyword; it remains as
417 a function. (Fortunately the function syntax was also accepted in
Guido van Rossum715287f2008-12-02 22:34:15 +0000418 2.x.) Also note that :func:`exec` no longer takes a stream argument;
419 instead of ``exec(f)`` you can use ``exec(f.read())``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000420
421* Integer literals no longer support a trailing ``l`` or ``L``.
422
423* String literals no longer support a leading ``u`` or ``U``.
424
Guido van Rossum715287f2008-12-02 22:34:15 +0000425* The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only
426 allowed at the module level, no longer inside functions.
427
428* The only acceptable syntax for relative imports is :keyword:`from`
429 ``.``[*module*] :keyword:`import` *name*; :keyword:`import` forms
430 not starting with ``.`` are always interpreted as absolute imports.
431 (:pep:`0328`)
432
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000433
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000434Changes Already Present In Python 2.6
435=====================================
436
Guido van Rossum715287f2008-12-02 22:34:15 +0000437Since many users presumably make the jump straight from Python 2.5 to
438Python 3.0, this section reminds the reader of new features that were
439originally designed for Python 3.0 but that were back-ported to Python
4402.6. The corresponding sections in :ref:`whats-new-in-2.6` should be
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000441consulted for longer descriptions.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000442
Guido van Rossum715287f2008-12-02 22:34:15 +0000443* :ref:`pep-0343`. The :keyword:`with` statement is now a standard
444 feature and no longer needs to be imported from the ``__future__``.
445 Also check out :ref:`new-26-context-managers` and
446 :ref:`new-module-contextlib`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000447
Guido van Rossum715287f2008-12-02 22:34:15 +0000448* :ref:`pep-0366`. This enhances the usefulness of the :option:`-m`
449 option when the referenced module lives in a package.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000450
Guido van Rossum715287f2008-12-02 22:34:15 +0000451* :ref:`pep-0370`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000452
Guido van Rossum715287f2008-12-02 22:34:15 +0000453* :ref:`pep-0371`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000454
Guido van Rossum715287f2008-12-02 22:34:15 +0000455* :ref:`pep-3101`. Note: the 2.6 description mentions the
456 :meth:`format` method for both 8-bit and Unicode strings. In 3.0,
457 only the :class:`str` type (text strings with Unicode support)
458 supports this method; the :class:`bytes` type does not. The plan is
459 to eventually make this the only API for string formatting, and to
460 start deprecating the ``%`` operator in Python 3.1.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000461
Guido van Rossum715287f2008-12-02 22:34:15 +0000462* :ref:`pep-3105`. This is now a standard feature and no longer needs
Guido van Rossum67d75ba2008-12-03 02:31:31 +0000463 to be imported from :mod:`__future__`. More details were given above.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000464
Guido van Rossum715287f2008-12-02 22:34:15 +0000465* :ref:`pep-3110`. The :keyword:`except` *exc* :keyword:`as` *var*
466 syntax is now standard and :keyword:`except` *exc*, *var* is no
467 longer supported. (Of course, the :keyword:`as` *var* part is still
468 optional.)
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000469
Guido van Rossum715287f2008-12-02 22:34:15 +0000470* :ref:`pep-3112`. The ``b"..."`` string literal notation (and its
471 variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now
Guido van Rossum38287682008-12-03 02:03:19 +0000472 produces a literal of type :class:`bytes`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000473
Guido van Rossum715287f2008-12-02 22:34:15 +0000474* :ref:`pep-3116`. The :mod:`io` module is now the standard way of
475 doing file I/O, and the initial values of :data:`sys.stdin`,
476 :data:`sys.stdout` and :data:`sys.stderr` are now instances of
477 :class:`io.TextIOBase`. The builtin :func:`open` function is now an
478 alias for :func:`io.open` and has additional keyword arguments
479 *encoding*, *errors*, *newline* and *closefd*. Also note that an
480 invalid *mode* argument now raises :exc:`ValueError`, not
Guido van Rossum38287682008-12-03 02:03:19 +0000481 :exc:`IOError`. The binary file object underlying a text file
482 object can be accessed as :attr:`f.buffer` (but beware that the
483 text object maintains a buffer of itself in order to speed up
484 the encoding and decoding operations).
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000485
Guido van Rossum715287f2008-12-02 22:34:15 +0000486* :ref:`pep-3118`. The old builtin :func:`buffer` is now really gone;
487 the new builtin :func:`memoryview` provides (mostly) similar
488 functionality.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000489
Guido van Rossum715287f2008-12-02 22:34:15 +0000490* :ref:`pep-3119`. The :mod:`abc` module and the ABCs defined in the
Guido van Rossum38287682008-12-03 02:03:19 +0000491 :mod:`collections` module plays a somewhat more prominent role in
Guido van Rossum715287f2008-12-02 22:34:15 +0000492 the language now, and builtin collection types like :class:`dict`
493 and :class:`list` conform to the :class:`collections.MutableMapping`
494 and :class:`collections.MutableSequence` ABC, respectively.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000495
Guido van Rossum715287f2008-12-02 22:34:15 +0000496* :ref:`pep-3127`. As mentioned above, the new octal literal
497 notation is the only one supported, and binary literals have been
498 added.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000499
Guido van Rossum38287682008-12-03 02:03:19 +0000500* :ref:`pep-3129`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000501
Guido van Rossum715287f2008-12-02 22:34:15 +0000502* :ref:`pep-3141`. The :mod:`numbers` module is another new use of
503 ABCs, defining Python's "numeric tower". Also note the new
Guido van Rossum38287682008-12-03 02:03:19 +0000504 :mod:`fractions` module which implements :class:`numbers.Rational`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000505
506
507Library Changes
508===============
509
Guido van Rossumc46ee542008-12-02 23:46:46 +0000510Due to time constraints, this document does not exhaustively cover the
511very extensive changes to the standard library. :pep:`3108` is the
512reference for the major changes to the library. Here's a capsule
513review:
Guido van Rossum56076da2008-12-02 22:58:36 +0000514
Guido van Rossumc46ee542008-12-02 23:46:46 +0000515* Many old modules were removed. Some, like :mod:`gopherlib` (no
516 longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were
517 already deprecated by :pep:`0004`. Others were removed as a result
518 of the removal of support for various platforms such as Irix, BeOS
519 and Mac OS 9 (see :pep:`0011`). Some modules were also selected for
520 removal in Python 3.0 due to lack of use or because a better
521 replacement exists. See :pep:`3108` for an exhaustive list.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000522
Guido van Rossumc46ee542008-12-02 23:46:46 +0000523* The :mod:`bsddb3` package was removed because its presence in the
524 core standard library has proved over time to be a particular burden
525 for the core developers due to testing instability and Berlekey DB's
526 release schedule. However, the package is alive and well,
527 externally maintained at http://www.jcea.es/programacion/pybsddb.htm.
528
529* Some modules were renamed because their old name flaunted
530 :pep:`0008`, or for various other reasons:
531
532 ======================= =======================
533 Old Name New Name
534 ======================= =======================
535 _winreg winreg
536 ConfigParser configparser
537 copy_reg copyreg
538 Queue queue
539 SocketServer socketserver
540 markupbase _markupbase
541 repr reprlib
542 test.test_support test.support
543 ======================= =======================
544
545* A common pattern in Python 2.x is to have one version of a module
546 implemented in pure Python, with an optional accelerated version
547 implemented as a C extension; for example, :mod:`pickle` and
548 :mod:`cPickle`. This places the burden of importing the accelerated
549 version and falling back on the pure Python version on each user of
550 these modules. In Python 3.0, the accelerated versions are
551 considered implementation details of the pure Python versions.
552 Users should always import the standard version, which attempts to
553 import the accelerated version and falls back to the pure Python
554 version. The :mod:`pickle` module received this treatment. The
555 :mod:`profile` module is on the list for 3.1. The :mod:`StringIO`
556 module has been turned into a class in the :mod:`io` module.
557
558* Some related modules have been grouped into packages, and usually
559 the submodule names have been simplified. The resulting new
560 packages are:
561
562 * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`,
563 :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`).
564
565 * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`).
566
567 * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`,
568 :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`,
569 :mod:`cookielib`).
570
571 * :mod:`tkinter` (all :mod:`Tkinter`-related modules except
572 :mod:`turtle`). The target audience of :mod:`turtle` doesn't
573 really care about :mod:`tkinter`. Also note that as of Python
574 2.6, the functionality of :mod:`turtle` has been greatly enhanced.
575
576 * :mod:`urllib` (:mod:`urllib`, :mod:`urllib`2, :mod:`urlparse`,
577 :mod:`robotparse`).
578
579 * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`,
580 :mod:`SimpleXMLRPCServer`).
581
582Some other library changes (not covered by :pep:`3108`):
Guido van Rossum715287f2008-12-02 22:34:15 +0000583
584* Killed :mod:`sets`. Use the builtin :func:`set` function.
585
Guido van Rossumc46ee542008-12-02 23:46:46 +0000586* Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`,
587 :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`,
588 :data:`sys.exc_traceback`. (Note that :data:`sys.last_type`
Guido van Rossum715287f2008-12-02 22:34:15 +0000589 etc. remain.)
590
Guido van Rossumc46ee542008-12-02 23:46:46 +0000591* Cleanup of the :class:`array.array` type: the :meth:`read` and
592 :meth:`write` methods are gone; use :meth:`fromfile` and
593 :meth:`tofile` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000594
Guido van Rossumc46ee542008-12-02 23:46:46 +0000595* Cleanup of the :mod:`operator` module: removed
596 :func:`sequenceIncludes` and :func:`isCallable`.
Guido van Rossum715287f2008-12-02 22:34:15 +0000597
Guido van Rossumc46ee542008-12-02 23:46:46 +0000598* Cleanup of the :mod:`thread` module: :func:`acquire_lock` and
599 :func:`release_lock` are gone; use :func:`acquire` and
600 :func:`release` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000601
Guido van Rossumc46ee542008-12-02 23:46:46 +0000602* Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API.
Guido van Rossum715287f2008-12-02 22:34:15 +0000603
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000604
Guido van Rossum715287f2008-12-02 22:34:15 +0000605:pep:`3101`: A New Approach To String Formatting
606================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000607
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000608* A new system for built-in string formatting operations replaces the
609 ``%`` string formatting operator. (However, the ``%`` operator is
610 still supported; it will be deprecated in Python 3.1 and removed
Guido van Rossum38287682008-12-03 02:03:19 +0000611 from the language at some later time.) Read :pep:`3101` for the full
612 scoop.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000613
614
Guido van Rossum715287f2008-12-02 22:34:15 +0000615:pep:`3106`: Revamping dict :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values`
616=========================================================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000617
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000618.. XXX expand this (but note that the "pitfalls" section currently has
619.. XXX more detail :-)
Georg Brandl396ef802008-02-02 10:30:18 +0000620
Georg Brandlec17d202008-02-02 10:44:37 +0000621* The :meth:`dict.iterkeys`, :meth:`dict.itervalues` and :meth:`dict.iteritems`
622 methods have been removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000623
Georg Brandlec17d202008-02-02 10:44:37 +0000624* :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` return objects
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000625 with set behavior that reference the underlying dict; these are often
626 referred to as *dictionary views*.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000627
628
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000629Exception Stuff
630===============
631
Guido van Rossum715287f2008-12-02 22:34:15 +0000632* :pep:`0352`: All exceptions must be derived (directly or indirectly)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000633 from :exc:`BaseException`. This is the root of the exception
634 hierarchy. Most exceptions should actually be derived from
635 :exc:`Exception`. This is not a new recommendation, but the
636 *requirement* to inherit from :exc:`BaseException` is new. (Python
637 2.6 still allowed classic classes to be raised, and placed no
638 restriction on what you can catch.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000639
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000640* :exc:`StandardError` was removed (in 2.6, actually).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000641
Georg Brandlec17d202008-02-02 10:44:37 +0000642* Dropping sequence behavior (slicing!) and :attr:`message` attribute of
Georg Brandl5a165582007-08-31 06:15:01 +0000643 exception instances.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000644
Guido van Rossum715287f2008-12-02 22:34:15 +0000645* :pep:`3109`: Raising exceptions. You must now use ``raise Exception(args)``
Georg Brandlec17d202008-02-02 10:44:37 +0000646 instead of ``raise Exception, args``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000647
Guido van Rossum715287f2008-12-02 22:34:15 +0000648* :pep:`3110`: Catching exceptions. You must now use ``except SomeException as
Georg Brandle06de8b2008-05-05 21:42:51 +0000649 identifier:`` instead of ``except Exception, identifier:``
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000650
Guido van Rossum715287f2008-12-02 22:34:15 +0000651* :pep:`3134`: Exception chaining. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000652
Georg Brandlec17d202008-02-02 10:44:37 +0000653* A few exception messages are improved when Windows fails to load an extension
654 module. For example, ``error code 193`` is now ``%1 is not a valid Win32
655 application``. Strings now deal with non-English locales.
Georg Brandl396ef802008-02-02 10:30:18 +0000656
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000657
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000658New Class And Metaclass Stuff
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000659=============================
660
Guido van Rossum38287682008-12-03 02:03:19 +0000661XXX Move to new syntax section???
662
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000663* Classic classes are gone.
664
Guido van Rossum38287682008-12-03 02:03:19 +0000665* :pep:`3115`: New Metaclass Syntax. Instead of::
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000666
Guido van Rossum38287682008-12-03 02:03:19 +0000667 class C:
668 __metaclass__ = M
669 ...
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000670
Guido van Rossum38287682008-12-03 02:03:19 +0000671 you now use::
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000672
Guido van Rossum38287682008-12-03 02:03:19 +0000673 class C(metaclass=M):
674 ...
675
676 The module-global :data:`__metaclass__` variable is no longer supported.
677 (It was a crutch to make it easier to default to new-style classes
678 without deriving every class from :class:`object`.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000679
680
Georg Brandl116aa622007-08-15 14:28:22 +0000681Other Language Changes
682======================
683
Guido van Rossum715287f2008-12-02 22:34:15 +0000684* Moved :func:`intern` to :func:`sys.intern`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000685
Georg Brandl396ef802008-02-02 10:30:18 +0000686* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
687 ``NotImplemented``.
688
Guido van Rossum715287f2008-12-02 22:34:15 +0000689* The concept of "unbound methods" was removed from the language.
690 When referencing a method as a class attribute, you now get a plain
691 function object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000692
Guido van Rossum715287f2008-12-02 22:34:15 +0000693* :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__`
694 were killed. The syntax ``a[i:j]`` now translates to
695 ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
696 :meth:`__delitem__`, when used as an assignment or deletion target,
697 respectively).
Georg Brandl396ef802008-02-02 10:30:18 +0000698
Guido van Rossum715287f2008-12-02 22:34:15 +0000699* :pep:`3111`: :func:`raw_input` renamed to :func:`input`. That is,
700 the new :func:`input` function reads a line from :data:`sys.stdin`
701 and returns it with the trailing newline stripped. It raises
702 :exc:`EOFError` if the input is terminated prematurely. To get the
703 old behavior of :func:`input`, use ``eval(input())``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000704
Guido van Rossum715287f2008-12-02 22:34:15 +0000705* :pep:`3114`: ``.next()`` renamed to :meth:`__next__`, new builtin
706 :func:`next` to call the :meth:`__next__` method on an object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000707
Guido van Rossum715287f2008-12-02 22:34:15 +0000708* :pep:`3135`: New :func:`super`. You can now invoke :func:`super`
709 without arguments and the right class and instance will
710 automatically be chosen. With arguments, its behavior is unchanged.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000711
Georg Brandlec17d202008-02-02 10:44:37 +0000712* :func:`zip`, :func:`map` and :func:`filter` return iterators.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000713
Georg Brandlec17d202008-02-02 10:44:37 +0000714* :data:`string.letters` and its friends (:data:`string.lowercase` and
Guido van Rossum715287f2008-12-02 22:34:15 +0000715 :data:`string.uppercase`) are gone. Use
716 :data:`string.ascii_letters` etc. instead. (The reason for the
717 removal is that :data:string.letters` and friends had
718 locale-specific behavior, which is a bad idea for such
719 attractively-named global "constants".)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000720
Guido van Rossum715287f2008-12-02 22:34:15 +0000721* Removed: :func:`apply`. Instead of ``apply(f, args)`` use
722 ``f(*args)``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000723
Guido van Rossum715287f2008-12-02 22:34:15 +0000724* Removed :func:`callable`. Instead of ``callable(f)`` you can use
725 ``hasattr(f, '__call__')``. The :func:`operator.isCallable` function
726 is also gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000727
Guido van Rossum715287f2008-12-02 22:34:15 +0000728* Removed :func:`coerce`. This function no longer serves a purpose
729 now that classic classes are gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000730
Guido van Rossum715287f2008-12-02 22:34:15 +0000731* Removed :func:`execfile`. Instead of ``execfile(fn)`` use
732 ``exec(open(fn).read())``.
Georg Brandl396ef802008-02-02 10:30:18 +0000733
Guido van Rossum715287f2008-12-02 22:34:15 +0000734* Removed :class:`file`. Use :func:`open`.
Georg Brandl396ef802008-02-02 10:30:18 +0000735
Guido van Rossum715287f2008-12-02 22:34:15 +0000736* Removed :func:`reduce`. Use :func:`functools.reduce` if you really
737 need it; however, 99 percent of the time an explicit :keyword:`for`
738 loop is more readable.
Georg Brandl396ef802008-02-02 10:30:18 +0000739
Guido van Rossum715287f2008-12-02 22:34:15 +0000740* Removed :func:`reload`. Use :func:`imp.reload`.
741
742* Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator
743 instead.
744
745* The :meth:`__oct__` and :meth:`__hex__` special methods are removed
746 -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert
747 the argument to an integer.
748
749* Removed support for :attr:`__members__` and :attr:`__methods__`.
750
751* Renamed the boolean conversion C-level slot and method:
752 ``nb_nonzero`` is now ``nb_bool`` and :meth:`__nonzero__` is now
753 :meth:`__bool__`.
754
755* Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the
756 underscores, adding an 's'). The :data:`__builtins__` variable
757 found in most global namespaces is unchanged. To modify a builtin,
758 you should use :mod:`builtins`, not :data:`__builtins__`!
759
760* Renamed function attributes :attr:`func_whatever` to
761 :attr:`__whatever__`. XXX list every single one.
762
763* Removed :exc:`StandardError`.
764
765* Removed METH_OLDARGS and WITH_CYCLE_GC. XXX more.
Georg Brandl116aa622007-08-15 14:28:22 +0000766
Georg Brandl5a165582007-08-31 06:15:01 +0000767.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000768
769
770Optimizations
771-------------
772
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000773The net result of the 3.0 generalizations is that Python 3.0 runs the
Guido van Rossum715287f2008-12-02 22:34:15 +0000774pystone benchmark around 10% slower than Python 2.5. Most likely the
775biggest cause is the removal of special-casing for small integers.
776There's room for improvement, but it will happen after 3.0 is
777released!
Georg Brandl116aa622007-08-15 14:28:22 +0000778
Georg Brandl5a165582007-08-31 06:15:01 +0000779.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000780
781
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000782New, Improved, And Deprecated Modules
Georg Brandl116aa622007-08-15 14:28:22 +0000783=====================================
784
Georg Brandlec17d202008-02-02 10:44:37 +0000785As usual, Python's standard library received a number of enhancements and bug
786fixes. Here's a partial list of the most notable changes, sorted alphabetically
787by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
788complete list of changes, or look through the Subversion logs for all the
789details.
Georg Brandl116aa622007-08-15 14:28:22 +0000790
Georg Brandlec17d202008-02-02 10:44:37 +0000791* The :mod:`cPickle` module is gone. Use :mod:`pickle` instead. Eventually
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000792 we'll have a transparent accelerator module.
Georg Brandl116aa622007-08-15 14:28:22 +0000793
Georg Brandlec17d202008-02-02 10:44:37 +0000794* The :mod:`imageop` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000795
Georg Brandlec17d202008-02-02 10:44:37 +0000796* The :mod:`audiodev`, :mod:`Bastion`, :mod:`bsddb185`, :mod:`exceptions`,
797 :mod:`linuxaudiodev`, :mod:`md5`, :mod:`MimeWriter`, :mod:`mimify`,
798 :mod:`popen2`, :mod:`rexec`, :mod:`sets`, :mod:`sha`, :mod:`stringold`,
799 :mod:`strop`, :mod:`sunaudiodev`, :mod:`timing`, and :mod:`xmllib` modules are
800 gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000801
Gregory P. Smith7b9a2222008-09-04 05:07:03 +0000802* The :mod:`bsddb` module is gone. It is being maintained externally
803 with its own release schedule better mirroring that of BerkeleyDB.
804 See http://www.jcea.es/programacion/pybsddb.htm.
805
Georg Brandlec17d202008-02-02 10:44:37 +0000806* The :mod:`new` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000807
Georg Brandlec17d202008-02-02 10:44:37 +0000808* The functions :func:`os.tmpnam`, :func:`os.tempnam` and :func:`os.tmpfile`
809 have been removed in favor of the :mod:`tempfile` module.
Georg Brandl396ef802008-02-02 10:30:18 +0000810
Trent Nelson428de652008-03-18 22:41:35 +0000811* The :mod:`tokenize` module has been changed to work with bytes. The main
812 entry point is now :func:`tokenize.tokenize`, instead of generate_tokens.
813
Georg Brandl5a165582007-08-31 06:15:01 +0000814.. ======================================================================
815.. whole new modules get described in subsections here
Georg Brandl116aa622007-08-15 14:28:22 +0000816
Georg Brandl5a165582007-08-31 06:15:01 +0000817.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000818
819
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000820Build And C API Changes
Georg Brandl116aa622007-08-15 14:28:22 +0000821=======================
822
823Changes to Python's build process and to the C API include:
824
Guido van Rossum715287f2008-12-02 22:34:15 +0000825* :pep:`3118`: New Buffer API. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000826
Guido van Rossum715287f2008-12-02 22:34:15 +0000827* :pep:`3121`: Extension Module Initialization & Finalization. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000828
Guido van Rossum715287f2008-12-02 22:34:15 +0000829* :pep:`3123`: Making :cmacro:`PyObject_HEAD` conform to standard C. XXX
Georg Brandl116aa622007-08-15 14:28:22 +0000830
Georg Brandl396ef802008-02-02 10:30:18 +0000831* No more C API support for restricted execution.
832
Georg Brandlec17d202008-02-02 10:44:37 +0000833* :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`, :cfunc:`PyMember_Get`,
834 and :cfunc:`PyMember_Set` C APIs are removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000835
Georg Brandlec17d202008-02-02 10:44:37 +0000836* New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like
837 :cfunc:`PyImport_ImportModule` but won't block on the import lock (returning
Georg Brandl396ef802008-02-02 10:30:18 +0000838 an error instead).
839
Georg Brandl5a165582007-08-31 06:15:01 +0000840.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000841
842
843Port-Specific Changes
844---------------------
845
Guido van Rossum715287f2008-12-02 22:34:15 +0000846XXX Platform-specific changes go here.
Georg Brandl116aa622007-08-15 14:28:22 +0000847
Guido van Rossum715287f2008-12-02 22:34:15 +0000848* XXX BeOS, RISCOS, Irix, Tru64 support
Georg Brandl396ef802008-02-02 10:30:18 +0000849
Georg Brandl5a165582007-08-31 06:15:01 +0000850.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000851
852
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000853Porting To Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +0000854=====================
855
Guido van Rossum56076da2008-12-02 22:58:36 +0000856For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
857best strategy is the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000858
Guido van Rossum56076da2008-12-02 22:58:36 +00008590. (Prerequisite:) Start with excellent test coverage.
Georg Brandl116aa622007-08-15 14:28:22 +0000860
Guido van Rossum56076da2008-12-02 22:58:36 +00008611. Port to Python 2.6. This should be no more work than the average
862 port from Python 2.x to Python 2.(x+1). Make sure all your tests
863 pass.
Christian Heimesf78b1c62007-12-02 16:52:32 +0000864
Guido van Rossum56076da2008-12-02 22:58:36 +00008652. (Still using 2.6:) Turn on the :option:`-3` command line switch.
866 This enables warnings about features that will be removed (or
867 change) in 3.0. Run your test suite again, and fix code that you
868 get warnings about until there are no warnings left, and all your
869 tests still pass.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000870
Guido van Rossum56076da2008-12-02 22:58:36 +00008713. Run the ``2to3`` source-to-source translator over your source code
872 tree. (See :ref:`2to3-reference` for more on this tool.) Run the
873 result of the translation under Python 3.0. Manually fix up any
874 remaining issues, fixing problems until all tests pass again.
875
876It is not recommended to try to write source code that runs unchanged
877under both Python 2.6 and 3.0; you'd have to use a very contorted
878coding style, e.g. avoiding :keyword:`print` statements, metaclasses,
879and much more. If you are maintaining a library that needs to support
880both Python 2.6 and Python 3.0, the best approach is to modify step 3
881above by editing the 2.6 version of the source code and running the
882``2to3`` translator again, rather than editing the 3.0 version of the
883source code.
884
885For porting C extensions to Python 3.0, please see :ref:`cporting-howto`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000886
Georg Brandl5a165582007-08-31 06:15:01 +0000887.. ======================================================================