blob: c195f53d2548d5112b381642708667b6647e6b22 [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 Rossum4a98a2a2008-11-21 18:35:43 +00007:Author: Guido van Rossum
8:Release: |release|
9:Date: |today|
10
11.. $Id$
12 Rules for maintenance:
Georg Brandl5a165582007-08-31 06:15:01 +000013
14 * Anyone can add text to this document. Do not spend very much time
15 on the wording of your changes, because your text will probably
16 get rewritten to some degree.
17
18 * The maintainer will go through Misc/NEWS periodically and add
19 changes; it's therefore more important to add your changes to
Guido van Rossum67d75ba2008-12-03 02:31:31 +000020 Misc/NEWS than to this file. (Note: I didn't get to this for 3.0.
21 GvR.)
Georg Brandl5a165582007-08-31 06:15:01 +000022
23 * This is not a complete list of every single change; completeness
24 is the purpose of Misc/NEWS. Some changes I consider too small
25 or esoteric to include. If such a change is added to the text,
26 I'll just remove it. (This is another reason you shouldn't spend
27 too much time on writing your addition.)
28
29 * If you want to draw your new text to the attention of the
30 maintainer, add 'XXX' to the beginning of the paragraph or
31 section.
32
33 * It's OK to just add a fragmentary note about a change. For
34 example: "XXX Describe the transmogrify() function added to the
35 socket module." The maintainer will research the change and
36 write the necessary text.
37
38 * You can comment out your additions if you like, but it's not
39 necessary (especially when a final release is some months away).
40
41 * Credit the author of a patch or bugfix. Just the name is
Guido van Rossum67d75ba2008-12-03 02:31:31 +000042 sufficient; the e-mail address isn't necessary. (Due to time
43 constraints I haven't managed to do this for 3.0. GvR.)
Georg Brandl5a165582007-08-31 06:15:01 +000044
45 * It's helpful to add the bug/patch number as a comment:
46
47 % Patch 12345
48 XXX Describe the transmogrify() function added to the socket
49 module.
50 (Contributed by P.Y. Developer.)
51
52 This saves the maintainer the effort of going through the SVN log
Guido van Rossum67d75ba2008-12-03 02:31:31 +000053 when researching a change. (Again, I didn't get to this for 3.0.
54 GvR.)
Georg Brandl116aa622007-08-15 14:28:22 +000055
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000056This article explains the new features in Python 3.0, compared to 2.6.
Guido van Rossumc46ee542008-12-02 23:46:46 +000057Python 3.0, also known as "Python 3000" or "Py3K", is the first ever
Guido van Rossum715287f2008-12-02 22:34:15 +000058*intentionally incompatible* release. There are more changes than in
59a typical release, and more that are important for all Python users.
60Nevertheless, after digesting the changes, you'll find that Python
61really hasn't changed all that much -- by and large, we're merely
62fixing well-known annoyances and warts.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000063
64This article doesn't attempt to provide a complete specification of
65the new features, but instead provides a convenient overview. For
66full details, you should refer to the documentation for Python 3.0. If
67you want to understand the complete implementation and design
68rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Georg Brandl5a165582007-08-31 06:15:01 +000070.. Compare with previous release in 2 - 3 sentences here.
71.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandl5a165582007-08-31 06:15:01 +000073.. ======================================================================
74.. Large, PEP-level features and changes should be described here.
75.. Should there be a new section here for 3k migration?
76.. Or perhaps a more general section describing module changes/deprecation?
77.. sets module deprecated
78.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Guido van Rossumb197f3c2007-08-31 00:37:00 +000081Common Stumbling Blocks
82=======================
83
Guido van Rossum715287f2008-12-02 22:34:15 +000084This section lists those few changes that are most likely to trip you
85up if you're used to Python 2.5.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000086
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000087Print Is A Function
88-------------------
Guido van Rossumdff1c312007-09-06 14:46:41 +000089
Guido van Rossum715287f2008-12-02 22:34:15 +000090The :keyword:`print` statement has been replaced with a :func:`print`
91function, with keyword arguments to replace most of the special syntax
92of the old :keyword:`print` statement (:pep:`3105`). Examples::
Guido van Rossumdff1c312007-09-06 14:46:41 +000093
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000094 Old: print "The answer is", 2*2
95 New: print("The answer is", 2*2)
Guido van Rossumdff1c312007-09-06 14:46:41 +000096
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000097 Old: print x, # Trailing comma suppresses newline
98 New: print(x, end=" ") # Appends a space instead of a newline
Guido van Rossumdff1c312007-09-06 14:46:41 +000099
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000100 Old: print # Prints a newline
101 New: print() # You must call the function!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000102
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000103 Old: print >>sys.stderr, "fatal error"
104 New: print("fatal error", file=sys.stderr)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000105
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000106 Old: print (x, y) # prints repr((x, y))
107 New: print((x, y)) # Not the same as print(x, y)!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000108
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000109You can also customize the separator between items, e.g.::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000110
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000111 print("There are <", 2**32, "> possibilities!", sep="")
Guido van Rossumdff1c312007-09-06 14:46:41 +0000112
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000113which produces::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000114
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000115 There are <4294967296> possibilities!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000116
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000117Note:
Guido van Rossumdff1c312007-09-06 14:46:41 +0000118
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000119* The :func:`print` function doesn't support the "softspace" feature of
Guido van Rossum715287f2008-12-02 22:34:15 +0000120 the old :keyword:`print` statement. For example, in Python 2.x,
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000121 ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
122 ``print("A\n", "B")`` writes ``"A\n B\n"``.
Guido van Rossumdff1c312007-09-06 14:46:41 +0000123
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000124* Initially, you'll be finding yourself typing the old ``print x``
125 a lot in interactive mode. Time to retrain your fingers to type
126 ``print(x)`` instead!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000127
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000128* When using the ``2to3`` source-to-source conversion tool, all
Guido van Rossum715287f2008-12-02 22:34:15 +0000129 :keyword:`print` statements are automatically converted to
130 :func:`print` function calls, so this is mostly a non-issue for
131 larger projects.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000132
Gregory P. Smithf3655c42008-12-02 23:52:53 +0000133Views And Iterators Instead Of Lists
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000134-------------------------------------
135
136Some well-known APIs no longer return lists:
137
138* :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and
139 :meth:`dict.values` return "views" instead of lists. For example,
140 this no longer works: ``k = d.keys(); k.sort()``. Use ``k =
Guido van Rossum73961352008-12-03 00:54:52 +0000141 sorted(d)`` instead (this works in Python 2.5 too, and is just
142 as efficient).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000143
144* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
145 :meth:`dict.itervalues` methods are no longer supported.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000146
Georg Brandlec17d202008-02-02 10:44:37 +0000147* :func:`map` and :func:`filter` return iterators. A quick fix is e.g.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000148 ``list(map(...))``, but a better fix is often to use a list
Georg Brandlec17d202008-02-02 10:44:37 +0000149 comprehension (especially when the original code uses :keyword:`lambda`).
150 Particularly tricky is :func:`map` invoked for the side effects of the
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000151 function; the correct transformation is to use a for-loop.
152
Guido van Rossum73961352008-12-03 00:54:52 +0000153* :func:`range` now behaves like :func:`xrange` used to behave, except
154 it works with values of arbitrary size. The latter no longer
155 exists.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000156
157* :func:`zip` now returns an iterator.
158
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000159Ordering Comparisons
160--------------------
161
162Python 3.0 has simplified the rules for ordering comparisons:
163
164* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
165 raise a TypeError exception when the operands don't have a
166 meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0
167 > None`` or ``len <= len`` are no longer valid. A corollary is that
168 sorting a heterogeneous list no longer makes sense -- all the
169 elements must be comparable to each other. Note that this does not
170 apply to the ``==`` and ``!=`` operators: objects of different
171 uncomparable types always compare unequal to each other, and an
Guido van Rossumfedd1402008-12-03 04:15:35 +0000172 object always compares equal to itself (i.e., ``x is y`` implies
173 ``x == y``; this is true even for *NaN*).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000174
Guido van Rossumfedd1402008-12-03 04:15:35 +0000175* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the
176 *cmp* argument providing a comparison function. Use the *key*
177 argument instead. N.B. the *key* and *reverse* arguments are now
178 "keyword-only".
Kurt B. Kaisera1401012008-02-13 16:09:27 +0000179
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000180* The :func:`cmp` function is gone, and the :meth:`__cmp__` special
181 method is no longer supported. Use :meth:`__lt__` for sorting,
182 :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as
Guido van Rossum73961352008-12-03 00:54:52 +0000183 needed. (If you really need the :func:`cmp` functionality, you
184 could use the expression ``(a > b) - (a < b)`` as the equivalent for
185 ``cmp(a, b)``.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000186
187Integers
188--------
189
Guido van Rossum73961352008-12-03 00:54:52 +0000190* :pep:`0237`: Essentially, :class:`long` renamed to :class:`int`.
191 That is, there is only one built-in integral type, named
192 :class:`int`; but it behaves mostly like the old :class:`long` type.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000193
Guido van Rossum73961352008-12-03 00:54:52 +0000194* :pep:`0238`: An expression like ``1/2`` returns a float. Use
195 ``1//2`` to get the truncating behavior. (The latter syntax has
196 existed for years, at least since Python 2.2.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000197
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000198* The :data:`sys.maxint` constant was removed, since there is no
199 longer a limit to the value of ints. However, :data:`sys.maxsize`
200 can be used as an integer larger than any practical list or string
201 index. It conforms to the implementation's "natural" integer size
202 and is typically the same as :data:`sys.maxint` in previous releases
203 on the same platform (assuming the same build options).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000204
Guido van Rossum73961352008-12-03 00:54:52 +0000205* The :func:`repr` of a long integer doesn't include the trailing ``L``
206 anymore, so code that unconditionally strips that character will
207 chop off the last digit instead. (Use :func:`str` instead.)
208
209* Octal literals are no longer of the form ``0720``; use ``0o720``
210 instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000211
Guido van Rossumfedd1402008-12-03 04:15:35 +0000212Text Vs. Data Instead Of Unicode Vs. 8-bit
213------------------------------------------
214
215Everything you thought you knew about binary data and Unicode has
216changed.
217
218* Python 3.0 uses the concepts of *text* and (binary) *data* instead
219 of Unicode strings and 8-bit strings. All text is Unicode; however
220 *encoded* Unicode is represented as binary data. The type used to
221 hold text is :class:`str`, the type used to hold data is
222 :class:`bytes`. The biggest difference with the 2.x situation is
223 that any attempt to mix text and data in Python 3.0 raises
224 :ext:`TypeError`, whereas if you were to mix Unicode and 8-bit
225 strings in Python 2.x, it would work if the 8-bit string happened to
226 contain only 7-bit (ASCII) bytes, but you would get
227 :ext:`UnicodeDecodeError` if it contained non-ASCII values. This
228 value-specific behavior has caused numerous sad faces over the
229 years.
230
231* As a consequence of this change in philosophy, pretty much all code
232 that uses Unicode, encodings or binary data most likely has to
233 change. The change is for the better, as in the 2.x world there
234 were numerous bugs having to do with mixing encoded and unencoded
235 text. To be prepared in Python 2.x, start using :class:`unicode`
236 for all unencoded text, and :class:`str` for binary or encoded data
237 only. Then the ``2to3`` tool will do most of the work for you.
238
239* You can no longer use ``u"..."`` literals for Unicode text.
240 However, you must use ``b"..."`` literals for binary data.
241
242* As the :class:`str` and :class:`bytes` types cannot be mixed, you
243 must always explicitly convert between them. Use :meth:`str.encode`
244 to go from :class:`str` to :class:`bytes`, and :meth:`bytes.decode`
245 to go from :class:`bytes` to :class:`str`. You can also use
246 ``bytes(s, encoding=...)`` and ``str(b, encoding=...)``,
247 respectively.
248
249* Like :class:`str`, the :class:`bytes` type is immutable. There is a
250 separate *mutable* type to hold buffered binary data,
251 :class:`bytearray`. Nearly all APIs that accept :class:`bytes` also
252 accept :class:`bytearray`. The mutable API is based on
253 :class:`collections.MutableSequence`.
254
255* All backslashes in raw string literals are interpreted literally.
256 This means that ``'\U'`` and ``'\u'`` escapes in raw strings are not
257 treated specially. For example, ``r'\u20ac'`` is a string of 6
258 characters in Python 3.0, whereas in 2.6, ``ur'\u20ac'`` was the
259 single "euro" character. (Of course, this change only affects raw
260 string literals; the euro character is ``'\u20ac'`` in Python 3.0.)
261
262* The builtin :class:`basestring` abstract type was removed. Use
263 :class:`str` instead. The :class:`str` and :class:`bytes` types
264 don't have functionality enough in common to warrant a shared base
265 class. The ``2to3`` tool (see below) replaces every occurrence of
266 :class:`basestring` with :class:`str`.
267
268* Files opened as text files (still the default mode for :func:`open`)
269 always use an encoding to map between strings (in memory) and bytes
270 (on disk). Binary files (opened with a ``b`` in the mode argument)
271 always use bytes in memory. This means that if a file is opened
272 using an incorrect mode or encoding, I/O will likely fail loudly,
273 instead of silently producing incorrect data. It also means that
274 even Unix users will have to specify the correct mode (text or
275 binary) when opening a file. There is a platform-dependent default
276 encoding, which on Unixy platforms can be set with the ``LANG``
277 environment variable (and sometimes also with some other
278 platform-specific locale-related environment variables). In many
279 cases, but not all, the system default is UTF-8; you should never
280 count on this default. Any application reading or writing more than
281 pure ASCII text should probably have a way to override the encoding.
282 There is no longer any need for using the encoding-aware streams
283 in the :mod:`codecs` module.
284
285* Filenames are passed to and returned from APIs as (Unicode) strings.
286 This can present platform-specific problems because on some
287 platforms filenames are arbitrary byte strings. (On the other hand,
288 on Windows filenames are natively stored as Unicode.) As a
289 work-around, most APIs (e.g. :func:`open` and many functions in the
290 :mod:`os` module) that take filenames accept :class:`bytes` objects
291 as well as strings, and a few APIs have a way to ask for a
292 :class:`bytes` return value. Thus, :func:`os.listdir` returns a
293 list of :class:`bytes` instances if the argument is a :class:`bytes`
294 instance, and :func:`os.getcwdu` returns the current working
295 directory as a :class:`bytes` instance. Note that when
296 :func:`os.listdir` returns a list of strings, filenames that
297 cannot be decoded properly are omitted rather than raising
298 :exc:`UnicodeError`.
299
300* Some system APIs like :data:`os.environ` and :data:`sys.argv` can
301 also present problems when the bytes made available by the system is
302 not interpretable using the default encoding. Setting the ``LANG``
303 variable and rerunning the program is probably the best approach.
304
305* :pep:`3138`: The :func:`repr` of a string no longer escapes
306 non-ASCII characters. It still escapes control characters and code
307 points with non-printable status in the Unicode standard, however.
308
309* :pep:`3120`: The default source encoding is now UTF-8.
310
311* :pep:`3131`: Non-ASCII letters are now allowed in identifiers.
312 (However, the standard library remains ASCII-only with the exception
313 of contributor names in comments.)
314
315* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead,
316 import the :mod:`io` module and use :class:`io.StringIO` or
317 :class:`io.BytesIO` for text and data respectively.
318
319* See also the :ref:`unicode-howto`, which was updated for Python 3.0.
320
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000321
Guido van Rossum73961352008-12-03 00:54:52 +0000322Overview Of Syntax Changes
323==========================
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000324
Guido van Rossum73961352008-12-03 00:54:52 +0000325This section gives a brief overview of every *syntactic* change in
326Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000327
Guido van Rossum38287682008-12-03 02:03:19 +0000328New Syntax
329----------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000330
Guido van Rossum73961352008-12-03 00:54:52 +0000331* :pep:`3107`: Function argument and return value annotations. This
332 provides a standardized way of annotating a function's parameters
333 and return value. There are no semantics attached to such
334 annotations except that they can be introspected at runtime using
335 the :attr:`__annotations__` attribute. The intent is to encourage
336 experimentation through metaclasses, decorators or frameworks.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000337
Guido van Rossum715287f2008-12-02 22:34:15 +0000338* :pep:`3102`: Keyword-only arguments. Named parameters occurring
339 after ``*args`` in the parameter list *must* be specified using
340 keyword syntax in the call. You can also use a bare ``*`` in the
341 parameter list to indicate that you don't accept a variable-length
342 argument list, but you do have keyword-only arguments.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000343
344* Keyword arguments are allowed after the list of base classes in a
345 class definition. This is used by the new convention for specifying
Guido van Rossumfedd1402008-12-03 04:15:35 +0000346 a metaclass (see next section), but can be used for other purposes
347 as well, as long as the metaclass supports it.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000348
Guido van Rossum715287f2008-12-02 22:34:15 +0000349* :pep:`3104`: :keyword:`nonlocal` statement. Using ``nonlocal x``
350 you can now assign directly to a variable in an outer (but
351 non-global) scope. :keyword:`nonlocal` is a new reserved word.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000352
Guido van Rossum715287f2008-12-02 22:34:15 +0000353* :pep:`3132`: Extended Iterable Unpacking. You can now write things
354 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
355 stuff``. The ``rest`` object is always a (possibly empty) list; the
356 right-hand side may be any iterable. Example::
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000357
Guido van Rossum715287f2008-12-02 22:34:15 +0000358 (a, *rest, b) = range(5)
359
360 This sets *a* to ``0``, *b* to ``4``, and \*rest to ``[1, 2, 3]``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000361
362* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
Guido van Rossum73961352008-12-03 00:54:52 +0000363 same thing as ``dict(stuff)`` but is more flexible. (This is
364 :pep:`0274` vindicated. :-)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000365
366* Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty
Guido van Rossum715287f2008-12-02 22:34:15 +0000367 dictionary; use ``set()`` for an empty set. Set comprehensions are
Guido van Rossum73961352008-12-03 00:54:52 +0000368 also supported; e.g., ``{x for x in stuff}`` means the same thing as
Guido van Rossum715287f2008-12-02 22:34:15 +0000369 ``set(stuff)`` but is more flexible.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000370
371* New octal literals, e.g. ``0o720`` (already in 2.6). The old octal
Walter Dörwaldeab34c92008-12-02 11:58:09 +0000372 literals (``0720``) are gone.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000373
374* New binary literals, e.g. ``0b1010`` (already in 2.6).
375
376* Bytes literals are introduced with a leading ``b`` or ``B``.
377
Guido van Rossum38287682008-12-03 02:03:19 +0000378Changed Syntax
379--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000380
Guido van Rossumfedd1402008-12-03 04:15:35 +0000381* :pep:`3109` and :pep:`3134`: new :keyword:`raise` statement syntax:
382 ``raise [expr [from expr]]``. See below.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000383
Guido van Rossum715287f2008-12-02 22:34:15 +0000384* :keyword:`as` and :keyword:`with` are now reserved words. (Since
385 2.6, actually.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000386
Guido van Rossum715287f2008-12-02 22:34:15 +0000387* :keyword:`True`, :keyword:`False`, and :keyword:`None` are reserved
388 words. (2.6 partially enforced the restrictions on :keyword:`None`
389 already.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000390
Guido van Rossum715287f2008-12-02 22:34:15 +0000391* Change from :keyword:`except` *exc*, *var* to
392 :keyword:`except` *exc* :keyword:`as` *var*. See :pep:`3110`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000393
Guido van Rossumfedd1402008-12-03 04:15:35 +0000394* :pep:`3115`: New Metaclass Syntax. Instead of::
395
396 class C:
397 __metaclass__ = M
398 ...
399
400 you must now use::
401
402 class C(metaclass=M):
403 ...
404
405 The module-global :data:`__metaclass__` variable is no longer
406 supported. (It was a crutch to make it easier to default to
407 new-style classes without deriving every class from
408 :class:`object`.)
409
Guido van Rossum715287f2008-12-02 22:34:15 +0000410* List comprehensions no longer support the syntactic form
411 ``[... for var in item1, item2, ...]``. Use
412 ``[... for var in (item1, item2, ...)]`` instead.
413 Also note that list comprehensions have different semantics: they
414 are closer to syntactic sugar for a generator expression inside a
415 :func:`list` constructor, and in particular the loop control
416 variables are no longer leaked into the surrounding scope.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000417
Guido van Rossum715287f2008-12-02 22:34:15 +0000418* The *ellipsis* (``...``) can be used as an atomic expression
419 anywhere. (Previously it was only allowed in slices.) Also, it
420 *must* now be spelled as ``...``. (Previously it could also be
421 spelled as ``. . .``, by a mere accident of the grammar.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000422
Guido van Rossum38287682008-12-03 02:03:19 +0000423Removed Syntax
424--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000425
Guido van Rossum715287f2008-12-02 22:34:15 +0000426* :pep:`3113`: Tuple parameter unpacking removed. You can no longer
427 write ``def foo(a, (b, c)): ...``.
428 Use ``def foo(a, b_c): b, c = b_c`` instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000429
Guido van Rossum715287f2008-12-02 22:34:15 +0000430* Removed backticks (use :func:`repr` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000431
Guido van Rossum715287f2008-12-02 22:34:15 +0000432* Removed ``<>`` (use ``!=`` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000433
434* Removed keyword: :func:`exec` is no longer a keyword; it remains as
435 a function. (Fortunately the function syntax was also accepted in
Guido van Rossum715287f2008-12-02 22:34:15 +0000436 2.x.) Also note that :func:`exec` no longer takes a stream argument;
437 instead of ``exec(f)`` you can use ``exec(f.read())``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000438
439* Integer literals no longer support a trailing ``l`` or ``L``.
440
441* String literals no longer support a leading ``u`` or ``U``.
442
Guido van Rossum715287f2008-12-02 22:34:15 +0000443* The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only
444 allowed at the module level, no longer inside functions.
445
446* The only acceptable syntax for relative imports is :keyword:`from`
447 ``.``[*module*] :keyword:`import` *name*; :keyword:`import` forms
448 not starting with ``.`` are always interpreted as absolute imports.
449 (:pep:`0328`)
450
Guido van Rossumfedd1402008-12-03 04:15:35 +0000451* Classic classes are gone.
452
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000453
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000454Changes Already Present In Python 2.6
455=====================================
456
Guido van Rossum715287f2008-12-02 22:34:15 +0000457Since many users presumably make the jump straight from Python 2.5 to
458Python 3.0, this section reminds the reader of new features that were
459originally designed for Python 3.0 but that were back-ported to Python
4602.6. The corresponding sections in :ref:`whats-new-in-2.6` should be
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000461consulted for longer descriptions.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000462
Guido van Rossum715287f2008-12-02 22:34:15 +0000463* :ref:`pep-0343`. The :keyword:`with` statement is now a standard
464 feature and no longer needs to be imported from the ``__future__``.
465 Also check out :ref:`new-26-context-managers` and
466 :ref:`new-module-contextlib`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000467
Guido van Rossum715287f2008-12-02 22:34:15 +0000468* :ref:`pep-0366`. This enhances the usefulness of the :option:`-m`
469 option when the referenced module lives in a package.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000470
Guido van Rossum715287f2008-12-02 22:34:15 +0000471* :ref:`pep-0370`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000472
Guido van Rossum715287f2008-12-02 22:34:15 +0000473* :ref:`pep-0371`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000474
Guido van Rossum715287f2008-12-02 22:34:15 +0000475* :ref:`pep-3101`. Note: the 2.6 description mentions the
476 :meth:`format` method for both 8-bit and Unicode strings. In 3.0,
477 only the :class:`str` type (text strings with Unicode support)
478 supports this method; the :class:`bytes` type does not. The plan is
479 to eventually make this the only API for string formatting, and to
480 start deprecating the ``%`` operator in Python 3.1.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000481
Guido van Rossum715287f2008-12-02 22:34:15 +0000482* :ref:`pep-3105`. This is now a standard feature and no longer needs
Guido van Rossum67d75ba2008-12-03 02:31:31 +0000483 to be imported from :mod:`__future__`. More details were given above.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000484
Guido van Rossum715287f2008-12-02 22:34:15 +0000485* :ref:`pep-3110`. The :keyword:`except` *exc* :keyword:`as` *var*
486 syntax is now standard and :keyword:`except` *exc*, *var* is no
487 longer supported. (Of course, the :keyword:`as` *var* part is still
488 optional.)
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000489
Guido van Rossum715287f2008-12-02 22:34:15 +0000490* :ref:`pep-3112`. The ``b"..."`` string literal notation (and its
491 variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now
Guido van Rossum38287682008-12-03 02:03:19 +0000492 produces a literal of type :class:`bytes`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000493
Guido van Rossum715287f2008-12-02 22:34:15 +0000494* :ref:`pep-3116`. The :mod:`io` module is now the standard way of
495 doing file I/O, and the initial values of :data:`sys.stdin`,
496 :data:`sys.stdout` and :data:`sys.stderr` are now instances of
497 :class:`io.TextIOBase`. The builtin :func:`open` function is now an
498 alias for :func:`io.open` and has additional keyword arguments
499 *encoding*, *errors*, *newline* and *closefd*. Also note that an
500 invalid *mode* argument now raises :exc:`ValueError`, not
Guido van Rossum38287682008-12-03 02:03:19 +0000501 :exc:`IOError`. The binary file object underlying a text file
502 object can be accessed as :attr:`f.buffer` (but beware that the
503 text object maintains a buffer of itself in order to speed up
504 the encoding and decoding operations).
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000505
Guido van Rossum715287f2008-12-02 22:34:15 +0000506* :ref:`pep-3118`. The old builtin :func:`buffer` is now really gone;
507 the new builtin :func:`memoryview` provides (mostly) similar
508 functionality.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000509
Guido van Rossum715287f2008-12-02 22:34:15 +0000510* :ref:`pep-3119`. The :mod:`abc` module and the ABCs defined in the
Guido van Rossum38287682008-12-03 02:03:19 +0000511 :mod:`collections` module plays a somewhat more prominent role in
Guido van Rossum715287f2008-12-02 22:34:15 +0000512 the language now, and builtin collection types like :class:`dict`
513 and :class:`list` conform to the :class:`collections.MutableMapping`
514 and :class:`collections.MutableSequence` ABC, respectively.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000515
Guido van Rossum715287f2008-12-02 22:34:15 +0000516* :ref:`pep-3127`. As mentioned above, the new octal literal
517 notation is the only one supported, and binary literals have been
518 added.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000519
Guido van Rossum38287682008-12-03 02:03:19 +0000520* :ref:`pep-3129`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000521
Guido van Rossum715287f2008-12-02 22:34:15 +0000522* :ref:`pep-3141`. The :mod:`numbers` module is another new use of
523 ABCs, defining Python's "numeric tower". Also note the new
Guido van Rossum38287682008-12-03 02:03:19 +0000524 :mod:`fractions` module which implements :class:`numbers.Rational`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000525
526
527Library Changes
528===============
529
Guido van Rossumc46ee542008-12-02 23:46:46 +0000530Due to time constraints, this document does not exhaustively cover the
531very extensive changes to the standard library. :pep:`3108` is the
532reference for the major changes to the library. Here's a capsule
533review:
Guido van Rossum56076da2008-12-02 22:58:36 +0000534
Guido van Rossumc46ee542008-12-02 23:46:46 +0000535* Many old modules were removed. Some, like :mod:`gopherlib` (no
536 longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were
537 already deprecated by :pep:`0004`. Others were removed as a result
538 of the removal of support for various platforms such as Irix, BeOS
539 and Mac OS 9 (see :pep:`0011`). Some modules were also selected for
540 removal in Python 3.0 due to lack of use or because a better
541 replacement exists. See :pep:`3108` for an exhaustive list.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000542
Guido van Rossumc46ee542008-12-02 23:46:46 +0000543* The :mod:`bsddb3` package was removed because its presence in the
544 core standard library has proved over time to be a particular burden
545 for the core developers due to testing instability and Berlekey DB's
546 release schedule. However, the package is alive and well,
547 externally maintained at http://www.jcea.es/programacion/pybsddb.htm.
548
549* Some modules were renamed because their old name flaunted
550 :pep:`0008`, or for various other reasons:
551
552 ======================= =======================
553 Old Name New Name
554 ======================= =======================
555 _winreg winreg
556 ConfigParser configparser
557 copy_reg copyreg
558 Queue queue
559 SocketServer socketserver
560 markupbase _markupbase
561 repr reprlib
562 test.test_support test.support
563 ======================= =======================
564
565* A common pattern in Python 2.x is to have one version of a module
566 implemented in pure Python, with an optional accelerated version
567 implemented as a C extension; for example, :mod:`pickle` and
568 :mod:`cPickle`. This places the burden of importing the accelerated
569 version and falling back on the pure Python version on each user of
570 these modules. In Python 3.0, the accelerated versions are
571 considered implementation details of the pure Python versions.
572 Users should always import the standard version, which attempts to
573 import the accelerated version and falls back to the pure Python
Guido van Rossumfedd1402008-12-03 04:15:35 +0000574 version. The :mod:`pickle` / :mod:`cPickle` pair received this
575 treatment. The :mod:`profile` module is on the list for 3.1. The
576 :mod:`StringIO` module has been turned into a class in the :mod:`io`
577 module.
Guido van Rossumc46ee542008-12-02 23:46:46 +0000578
579* Some related modules have been grouped into packages, and usually
580 the submodule names have been simplified. The resulting new
581 packages are:
582
583 * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`,
584 :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`).
585
586 * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`).
587
588 * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`,
589 :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`,
590 :mod:`cookielib`).
591
592 * :mod:`tkinter` (all :mod:`Tkinter`-related modules except
593 :mod:`turtle`). The target audience of :mod:`turtle` doesn't
594 really care about :mod:`tkinter`. Also note that as of Python
595 2.6, the functionality of :mod:`turtle` has been greatly enhanced.
596
597 * :mod:`urllib` (:mod:`urllib`, :mod:`urllib`2, :mod:`urlparse`,
598 :mod:`robotparse`).
599
600 * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`,
601 :mod:`SimpleXMLRPCServer`).
602
Guido van Rossumfedd1402008-12-03 04:15:35 +0000603Some other changes to standard library moduled, not covered by
604:pep:`3108`:
Guido van Rossum715287f2008-12-02 22:34:15 +0000605
606* Killed :mod:`sets`. Use the builtin :func:`set` function.
607
Guido van Rossumc46ee542008-12-02 23:46:46 +0000608* Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`,
609 :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`,
610 :data:`sys.exc_traceback`. (Note that :data:`sys.last_type`
Guido van Rossum715287f2008-12-02 22:34:15 +0000611 etc. remain.)
612
Guido van Rossumc46ee542008-12-02 23:46:46 +0000613* Cleanup of the :class:`array.array` type: the :meth:`read` and
614 :meth:`write` methods are gone; use :meth:`fromfile` and
615 :meth:`tofile` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000616
Guido van Rossumc46ee542008-12-02 23:46:46 +0000617* Cleanup of the :mod:`operator` module: removed
618 :func:`sequenceIncludes` and :func:`isCallable`.
Guido van Rossum715287f2008-12-02 22:34:15 +0000619
Guido van Rossumc46ee542008-12-02 23:46:46 +0000620* Cleanup of the :mod:`thread` module: :func:`acquire_lock` and
621 :func:`release_lock` are gone; use :func:`acquire` and
622 :func:`release` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000623
Guido van Rossumc46ee542008-12-02 23:46:46 +0000624* Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API.
Guido van Rossum715287f2008-12-02 22:34:15 +0000625
Guido van Rossumfedd1402008-12-03 04:15:35 +0000626* The :mod:`new` module is gone.
627
628* The functions :func:`os.tmpnam`, :func:`os.tempnam` and
629 :func:`os.tmpfile` have been removed in favor of the :mod:`tempfile`
630 module.
631
632* The :mod:`tokenize` module has been changed to work with bytes. The
633 main entry point is now :func:`tokenize.tokenize`, instead of
634 generate_tokens.
635
636* :data:`string.letters` and its friends (:data:`string.lowercase` and
637 :data:`string.uppercase`) are gone. Use
638 :data:`string.ascii_letters` etc. instead. (The reason for the
639 removal is that :data:string.letters` and friends had
640 locale-specific behavior, which is a bad idea for such
641 attractively-named global "constants".)
642
643* Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the
644 underscores, adding an 's'). The :data:`__builtins__` variable
645 found in most global namespaces is unchanged. To modify a builtin,
646 you should use :mod:`builtins`, not :data:`__builtins__`!
647
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000648
Guido van Rossum715287f2008-12-02 22:34:15 +0000649:pep:`3101`: A New Approach To String Formatting
650================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000651
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000652* A new system for built-in string formatting operations replaces the
653 ``%`` string formatting operator. (However, the ``%`` operator is
654 still supported; it will be deprecated in Python 3.1 and removed
Guido van Rossum38287682008-12-03 02:03:19 +0000655 from the language at some later time.) Read :pep:`3101` for the full
656 scoop.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000657
658
Guido van Rossumfedd1402008-12-03 04:15:35 +0000659Changes To Exceptions
660=====================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000661
Guido van Rossumfedd1402008-12-03 04:15:35 +0000662The APIs for raising and catching exception have been cleaned up and
663new powerful features added:
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000664
Guido van Rossum715287f2008-12-02 22:34:15 +0000665* :pep:`0352`: All exceptions must be derived (directly or indirectly)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000666 from :exc:`BaseException`. This is the root of the exception
Guido van Rossumfedd1402008-12-03 04:15:35 +0000667 hierarchy. This is not new as a recommendation, but the
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000668 *requirement* to inherit from :exc:`BaseException` is new. (Python
669 2.6 still allowed classic classes to be raised, and placed no
Guido van Rossumfedd1402008-12-03 04:15:35 +0000670 restriction on what you can catch.) As a consequence, string
671 exceptions are finally truly and utterly dead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000672
Guido van Rossumfedd1402008-12-03 04:15:35 +0000673* Almost all exceptions should actually derive from :exc:`Exception`;
674 :exc:`BaseException` should only be used as a base class for
675 exceptions that should only be handled at the top level, such as
676 :exc:`SystemExit` or :exc:`KeyboardInterrupt`. The recommended
677 idiom for handling all exceptions except for this latter category is
678 to use :keyword:`except` :exc:`Exception`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000679
Guido van Rossumfedd1402008-12-03 04:15:35 +0000680* :exc:`StandardError` was removed (in 2.6 already).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000681
Guido van Rossumfedd1402008-12-03 04:15:35 +0000682* Exceptions no longer behave as sequences. Use the :attr:`args`
683 attribute instead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000684
Guido van Rossumfedd1402008-12-03 04:15:35 +0000685* :pep:`3109`: Raising exceptions. You must now use :keyword:`raise`
686 *Exception*(*args*) instead of :keyword:`raise` *Exception*, *args*.
687 Additionally, you can no longer explicitly specify a traceback;
688 instead, if you *have* to do this, you can assign directly to the
689 :attr:`__traceback__` attribute (see below).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000690
Guido van Rossumfedd1402008-12-03 04:15:35 +0000691* :pep:`3110`: Catching exceptions. You must now use
692 *:keyword:`except` SomeException* :keyword:`as` *variable* instead
693 *of :keyword:`except` *SomeException*, variable*. Moreover, the
694 *variable* is explicitly deleted when the :keyword:`except` block
695 is left.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000696
Guido van Rossumfedd1402008-12-03 04:15:35 +0000697* :pep:`3134`: Exception chaining. There are two cases: implicit
698 chaining and explicit chaining. Implicit chaining happens when an
699 exception is raised in an :keyword:`except` or :keyword:`finally`
700 handler block. This usually happens due to a bug in the handler
701 block; we call this a *secondary* exception. In this case, the
702 original exception (that was being handled) is saved as the
703 :attr:`__context__` attribute of the secondary exception.
704 Explicit chaining is invoked with this syntax::
705
706 raise SecondaryException() from primary_exception
707
708 (where *primary_exception* is any expression that produces an
709 exception object, probably an exception that was previously caught).
710 In this case, the primary exception is stored on the
711 :attr:`__cause__` attribute of the secondary exception. The
712 traceback printed when an unhandled exception occurs walks the chain
713 of :attr:`__cause__` and :attr:`__context__` attributes and prints a
714 separate traceback for each component of the chain, with the primary
715 exception at the top. (Java users may recognize this behavior. :-)
716
717* :pep:`3134`: Exception objects now store their traceback as the
718 :attr:`__traceback__` attribute. This means that an exception
719 object now contains all the information pertaining to an exception,
720 and there are fewer reasons to use :func:`sys.exc_info` (though the
721 latter is not removed).
722
723* A few exception messages are improved when Windows fails to load an
724 extension module. For example, ``error code 193`` is now ``%1 is
725 not a valid Win32 application``. Strings now deal with non-English
726 locales.
Georg Brandl396ef802008-02-02 10:30:18 +0000727
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000728
Guido van Rossumfedd1402008-12-03 04:15:35 +0000729Miscellaneous Other Changes
730===========================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000731
Guido van Rossumfedd1402008-12-03 04:15:35 +0000732Operators And Special Methods
733-----------------------------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000734
Georg Brandl396ef802008-02-02 10:30:18 +0000735* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
Guido van Rossumfedd1402008-12-03 04:15:35 +0000736 :data:`NotImplemented`.
Georg Brandl396ef802008-02-02 10:30:18 +0000737
Guido van Rossumfedd1402008-12-03 04:15:35 +0000738* The concept of "unbound methods" has been removed from the language.
Guido van Rossum715287f2008-12-02 22:34:15 +0000739 When referencing a method as a class attribute, you now get a plain
740 function object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000741
Guido van Rossum715287f2008-12-02 22:34:15 +0000742* :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__`
743 were killed. The syntax ``a[i:j]`` now translates to
744 ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
745 :meth:`__delitem__`, when used as an assignment or deletion target,
746 respectively).
Georg Brandl396ef802008-02-02 10:30:18 +0000747
Guido van Rossumfedd1402008-12-03 04:15:35 +0000748* :pep:`3114`: the standard :meth:`next` method has been renamed to
749 :meth:`__next__`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000750
Guido van Rossumfedd1402008-12-03 04:15:35 +0000751* The :meth:`__oct__` and :meth:`__hex__` special methods are removed
752 -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert
753 the argument to an integer.
754
755* Removed support for :attr:`__members__` and :attr:`__methods__`.
756
757* The function attributes named :attr:`func_X` have been renamed to
758 use the :data:`__X__` form, freeing up these names in the function
759 attribute namespace for user-defined attributes. To wit,
760 :attr:`func_closure`, :attr:`func_code`, :attr:`func_defaults`,
761 :attr:`func_dict`, :attr:`func_doc`, :attr:`func_globals`,
762 :attr:`func_name` were renamed to :attr:`__closure__`,
763 :attr:`__code__`, :attr:`__defaults__`, :attr:`__dict__`,
764 :attr:`__doc__`, :attr:`__globals__`, :attr:`__name__`,
765 respectively.
766
767* :meth:`__nonzero__` is now :meth:`__bool__`.
768
769Builtins
770--------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000771
Guido van Rossum715287f2008-12-02 22:34:15 +0000772* :pep:`3135`: New :func:`super`. You can now invoke :func:`super`
Guido van Rossumfedd1402008-12-03 04:15:35 +0000773 without arguments and (assuming this is in a regular instance method
774 defined inside a :keyword:`class` statement) the right class and
775 instance will automatically be chosen. With arguments, the behavior
776 of :func:`super` is unchanged.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000777
Guido van Rossumfedd1402008-12-03 04:15:35 +0000778* :pep:`3111`: :func:`raw_input` was renamed to :func:`input`. That
779 is, the new :func:`input` function reads a line from
780 :data:`sys.stdin` and returns it with the trailing newline stripped.
781 It raises :exc:`EOFError` if the input is terminated prematurely.
782 To get the old behavior of :func:`input`, use ``eval(input())``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000783
Guido van Rossumfedd1402008-12-03 04:15:35 +0000784* A new builtin :func:`next` was added to call the :meth:`__next__`
785 method on an object.
786
787* Moved :func:`intern` to :func:`sys.intern`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000788
Guido van Rossum715287f2008-12-02 22:34:15 +0000789* Removed: :func:`apply`. Instead of ``apply(f, args)`` use
790 ``f(*args)``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000791
Guido van Rossum715287f2008-12-02 22:34:15 +0000792* Removed :func:`callable`. Instead of ``callable(f)`` you can use
793 ``hasattr(f, '__call__')``. The :func:`operator.isCallable` function
794 is also gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000795
Guido van Rossum715287f2008-12-02 22:34:15 +0000796* Removed :func:`coerce`. This function no longer serves a purpose
797 now that classic classes are gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000798
Guido van Rossum715287f2008-12-02 22:34:15 +0000799* Removed :func:`execfile`. Instead of ``execfile(fn)`` use
800 ``exec(open(fn).read())``.
Georg Brandl396ef802008-02-02 10:30:18 +0000801
Guido van Rossum715287f2008-12-02 22:34:15 +0000802* Removed :class:`file`. Use :func:`open`.
Georg Brandl396ef802008-02-02 10:30:18 +0000803
Guido van Rossum715287f2008-12-02 22:34:15 +0000804* Removed :func:`reduce`. Use :func:`functools.reduce` if you really
805 need it; however, 99 percent of the time an explicit :keyword:`for`
806 loop is more readable.
Georg Brandl396ef802008-02-02 10:30:18 +0000807
Guido van Rossum715287f2008-12-02 22:34:15 +0000808* Removed :func:`reload`. Use :func:`imp.reload`.
809
810* Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator
811 instead.
812
Guido van Rossumfedd1402008-12-03 04:15:35 +0000813.. ======================================================================
Guido van Rossum715287f2008-12-02 22:34:15 +0000814
Guido van Rossumfedd1402008-12-03 04:15:35 +0000815
816Build and C API Changes
817=======================
818
819Due to time constraints, here is a *very* incomplete list of changes
820to the C API.
821
822* Support for several platforms was dropped, including but not limited
823 to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.
824
825* :pep:`3118`: New Buffer API.
826
827* :pep:`3121`: Extension Module Initialization & Finalization.
828
829* :pep:`3123`: Making :cmacro:`PyObject_HEAD` conform to standard C.
830
831* No more C API support for restricted execution.
832
833* :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`,
834 :cfunc:`PyMember_Get`, and :cfunc:`PyMember_Set` C APIs are removed.
835
836* New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like
837 :cfunc:`PyImport_ImportModule` but won't block on the import lock
838 (returning an error instead).
Guido van Rossum715287f2008-12-02 22:34:15 +0000839
840* Renamed the boolean conversion C-level slot and method:
Guido van Rossumfedd1402008-12-03 04:15:35 +0000841 ``nb_nonzero`` is now ``nb_bool``.
Guido van Rossum715287f2008-12-02 22:34:15 +0000842
Guido van Rossumfedd1402008-12-03 04:15:35 +0000843* Removed ``METH_OLDARGS`` and ``WITH_CYCLE_GC`` from the C API.
Georg Brandl116aa622007-08-15 14:28:22 +0000844
Georg Brandl5a165582007-08-31 06:15:01 +0000845.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000846
847
Guido van Rossumfedd1402008-12-03 04:15:35 +0000848Performance
849===========
Georg Brandl116aa622007-08-15 14:28:22 +0000850
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000851The net result of the 3.0 generalizations is that Python 3.0 runs the
Guido van Rossum715287f2008-12-02 22:34:15 +0000852pystone benchmark around 10% slower than Python 2.5. Most likely the
853biggest cause is the removal of special-casing for small integers.
854There's room for improvement, but it will happen after 3.0 is
855released!
Georg Brandl116aa622007-08-15 14:28:22 +0000856
Georg Brandl5a165582007-08-31 06:15:01 +0000857.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000858
859
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000860Porting To Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +0000861=====================
862
Guido van Rossum56076da2008-12-02 22:58:36 +0000863For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
864best strategy is the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Guido van Rossum56076da2008-12-02 22:58:36 +00008660. (Prerequisite:) Start with excellent test coverage.
Georg Brandl116aa622007-08-15 14:28:22 +0000867
Guido van Rossum56076da2008-12-02 22:58:36 +00008681. Port to Python 2.6. This should be no more work than the average
869 port from Python 2.x to Python 2.(x+1). Make sure all your tests
870 pass.
Christian Heimesf78b1c62007-12-02 16:52:32 +0000871
Guido van Rossum56076da2008-12-02 22:58:36 +00008722. (Still using 2.6:) Turn on the :option:`-3` command line switch.
873 This enables warnings about features that will be removed (or
874 change) in 3.0. Run your test suite again, and fix code that you
875 get warnings about until there are no warnings left, and all your
876 tests still pass.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000877
Guido van Rossum56076da2008-12-02 22:58:36 +00008783. Run the ``2to3`` source-to-source translator over your source code
879 tree. (See :ref:`2to3-reference` for more on this tool.) Run the
880 result of the translation under Python 3.0. Manually fix up any
881 remaining issues, fixing problems until all tests pass again.
882
883It is not recommended to try to write source code that runs unchanged
884under both Python 2.6 and 3.0; you'd have to use a very contorted
885coding style, e.g. avoiding :keyword:`print` statements, metaclasses,
886and much more. If you are maintaining a library that needs to support
887both Python 2.6 and Python 3.0, the best approach is to modify step 3
888above by editing the 2.6 version of the source code and running the
889``2to3`` translator again, rather than editing the 3.0 version of the
890source code.
891
892For porting C extensions to Python 3.0, please see :ref:`cporting-howto`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000893
Georg Brandl5a165582007-08-31 06:15:01 +0000894.. ======================================================================