blob: 3b0964cef106273ff94c713408a5174d45a07d2d [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 Rossum08388ef2008-12-03 05:39:28 +000058*intentionally backwards incompatible* Python release. There are more
59changes than in a typical release, and more that are important for all
60Python users. Nevertheless, after digesting the changes, you'll find
61that Python really hasn't changed all that much -- by and large, we're
62mostly fixing well-known annoyances and warts, and removing a lot of
63old cruft.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000064
65This article doesn't attempt to provide a complete specification of
Guido van Rossum08388ef2008-12-03 05:39:28 +000066all new features, but instead tries to give a convenient overview.
67For full details, you should refer to the documentation for Python
683.0, and/or the many PEPs referenced in the text. If you want to
69understand the complete implementation and design rationale for a
70particular feature, PEPs usually have more details than the regular
71documentation; but note that PEPs usually are not kept up-to-date once
72a feature has been fully implemented.
73
74Due to time constraints this document is not as complete as it should
75have been. As always for a new release, the ``Misc/NEWS`` file in the
76source distribution contains a wealth of detailed information about
77every small thing that was changed.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Georg Brandl5a165582007-08-31 06:15:01 +000079.. Compare with previous release in 2 - 3 sentences here.
80.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000081
Georg Brandl5a165582007-08-31 06:15:01 +000082.. ======================================================================
83.. Large, PEP-level features and changes should be described here.
84.. Should there be a new section here for 3k migration?
85.. Or perhaps a more general section describing module changes/deprecation?
86.. sets module deprecated
87.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
Guido van Rossumb197f3c2007-08-31 00:37:00 +000090Common Stumbling Blocks
91=======================
92
Guido van Rossum715287f2008-12-02 22:34:15 +000093This section lists those few changes that are most likely to trip you
94up if you're used to Python 2.5.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000095
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000096Print Is A Function
97-------------------
Guido van Rossumdff1c312007-09-06 14:46:41 +000098
Guido van Rossum715287f2008-12-02 22:34:15 +000099The :keyword:`print` statement has been replaced with a :func:`print`
100function, with keyword arguments to replace most of the special syntax
101of the old :keyword:`print` statement (:pep:`3105`). Examples::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000102
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000103 Old: print "The answer is", 2*2
104 New: print("The answer is", 2*2)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000105
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000106 Old: print x, # Trailing comma suppresses newline
107 New: print(x, end=" ") # Appends a space instead of a newline
Guido van Rossumdff1c312007-09-06 14:46:41 +0000108
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000109 Old: print # Prints a newline
110 New: print() # You must call the function!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000111
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000112 Old: print >>sys.stderr, "fatal error"
113 New: print("fatal error", file=sys.stderr)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000114
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000115 Old: print (x, y) # prints repr((x, y))
116 New: print((x, y)) # Not the same as print(x, y)!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000117
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000118You can also customize the separator between items, e.g.::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000119
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000120 print("There are <", 2**32, "> possibilities!", sep="")
Guido van Rossumdff1c312007-09-06 14:46:41 +0000121
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000122which produces::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000123
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000124 There are <4294967296> possibilities!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000125
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000126Note:
Guido van Rossumdff1c312007-09-06 14:46:41 +0000127
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000128* The :func:`print` function doesn't support the "softspace" feature of
Guido van Rossum715287f2008-12-02 22:34:15 +0000129 the old :keyword:`print` statement. For example, in Python 2.x,
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000130 ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
131 ``print("A\n", "B")`` writes ``"A\n B\n"``.
Guido van Rossumdff1c312007-09-06 14:46:41 +0000132
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000133* Initially, you'll be finding yourself typing the old ``print x``
134 a lot in interactive mode. Time to retrain your fingers to type
135 ``print(x)`` instead!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000136
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000137* When using the ``2to3`` source-to-source conversion tool, all
Guido van Rossum715287f2008-12-02 22:34:15 +0000138 :keyword:`print` statements are automatically converted to
139 :func:`print` function calls, so this is mostly a non-issue for
140 larger projects.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000141
Gregory P. Smithf3655c42008-12-02 23:52:53 +0000142Views And Iterators Instead Of Lists
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000143-------------------------------------
144
145Some well-known APIs no longer return lists:
146
147* :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and
148 :meth:`dict.values` return "views" instead of lists. For example,
149 this no longer works: ``k = d.keys(); k.sort()``. Use ``k =
Raymond Hettingerab4c51c2008-12-03 15:04:01 +0000150 sorted(d)`` instead (this works in Python 2.5 too and is just
Guido van Rossum73961352008-12-03 00:54:52 +0000151 as efficient).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000152
153* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
154 :meth:`dict.itervalues` methods are no longer supported.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000155
Guido van Rossum08388ef2008-12-03 05:39:28 +0000156* :func:`map` and :func:`filter` return iterators. If you really need
157 a list, a quick fix is e.g. ``list(map(...))``, but a better fix is
158 often to use a list comprehension (especially when the original code
159 uses :keyword:`lambda`), or rewriting the code so it doesn't need a
160 list at all. Particularly tricky is :func:`map` invoked for the
161 side effects of the function; the correct transformation is to use a
162 regular :keyword:`for` loop (since creating a list would just be
163 wasteful).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000164
Guido van Rossum73961352008-12-03 00:54:52 +0000165* :func:`range` now behaves like :func:`xrange` used to behave, except
166 it works with values of arbitrary size. The latter no longer
167 exists.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000168
169* :func:`zip` now returns an iterator.
170
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000171Ordering Comparisons
172--------------------
173
174Python 3.0 has simplified the rules for ordering comparisons:
175
176* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
177 raise a TypeError exception when the operands don't have a
178 meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0
Guido van Rossum08388ef2008-12-03 05:39:28 +0000179 > None`` or ``len <= len`` are no longer valid, and e.g. ``None <
180 None`` raises :exc:`TypeError` instead of returning
181 :keyword:`False`. A corollary is that sorting a heterogeneous list
182 no longer makes sense -- all the elements must be comparable to each
183 other. Note that this does not apply to the ``==`` and ``!=``
184 operators: objects of different incomparable types always compare
Guido van Rossum815427c2008-12-03 15:24:50 +0000185 unequal to each other.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000186
Guido van Rossumfedd1402008-12-03 04:15:35 +0000187* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the
188 *cmp* argument providing a comparison function. Use the *key*
189 argument instead. N.B. the *key* and *reverse* arguments are now
190 "keyword-only".
Kurt B. Kaisera1401012008-02-13 16:09:27 +0000191
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000192* The :func:`cmp` function is gone, and the :meth:`__cmp__` special
193 method is no longer supported. Use :meth:`__lt__` for sorting,
194 :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as
Guido van Rossum73961352008-12-03 00:54:52 +0000195 needed. (If you really need the :func:`cmp` functionality, you
196 could use the expression ``(a > b) - (a < b)`` as the equivalent for
197 ``cmp(a, b)``.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000198
199Integers
200--------
201
Guido van Rossum73961352008-12-03 00:54:52 +0000202* :pep:`0237`: Essentially, :class:`long` renamed to :class:`int`.
203 That is, there is only one built-in integral type, named
204 :class:`int`; but it behaves mostly like the old :class:`long` type.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000205
Guido van Rossum73961352008-12-03 00:54:52 +0000206* :pep:`0238`: An expression like ``1/2`` returns a float. Use
207 ``1//2`` to get the truncating behavior. (The latter syntax has
208 existed for years, at least since Python 2.2.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000209
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000210* The :data:`sys.maxint` constant was removed, since there is no
Guido van Rossum08388ef2008-12-03 05:39:28 +0000211 longer a limit to the value of integers. However, :data:`sys.maxsize`
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000212 can be used as an integer larger than any practical list or string
213 index. It conforms to the implementation's "natural" integer size
214 and is typically the same as :data:`sys.maxint` in previous releases
215 on the same platform (assuming the same build options).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000216
Guido van Rossum73961352008-12-03 00:54:52 +0000217* The :func:`repr` of a long integer doesn't include the trailing ``L``
218 anymore, so code that unconditionally strips that character will
219 chop off the last digit instead. (Use :func:`str` instead.)
220
221* Octal literals are no longer of the form ``0720``; use ``0o720``
222 instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000223
Guido van Rossumfedd1402008-12-03 04:15:35 +0000224Text Vs. Data Instead Of Unicode Vs. 8-bit
225------------------------------------------
226
227Everything you thought you knew about binary data and Unicode has
228changed.
229
230* Python 3.0 uses the concepts of *text* and (binary) *data* instead
231 of Unicode strings and 8-bit strings. All text is Unicode; however
232 *encoded* Unicode is represented as binary data. The type used to
233 hold text is :class:`str`, the type used to hold data is
234 :class:`bytes`. The biggest difference with the 2.x situation is
235 that any attempt to mix text and data in Python 3.0 raises
Guido van Rossumb768d4f2008-12-03 04:18:17 +0000236 :exc:`TypeError`, whereas if you were to mix Unicode and 8-bit
Guido van Rossumfedd1402008-12-03 04:15:35 +0000237 strings in Python 2.x, it would work if the 8-bit string happened to
238 contain only 7-bit (ASCII) bytes, but you would get
Guido van Rossumb768d4f2008-12-03 04:18:17 +0000239 :exc:`UnicodeDecodeError` if it contained non-ASCII values. This
Guido van Rossumfedd1402008-12-03 04:15:35 +0000240 value-specific behavior has caused numerous sad faces over the
241 years.
242
243* As a consequence of this change in philosophy, pretty much all code
244 that uses Unicode, encodings or binary data most likely has to
245 change. The change is for the better, as in the 2.x world there
246 were numerous bugs having to do with mixing encoded and unencoded
247 text. To be prepared in Python 2.x, start using :class:`unicode`
248 for all unencoded text, and :class:`str` for binary or encoded data
249 only. Then the ``2to3`` tool will do most of the work for you.
250
251* You can no longer use ``u"..."`` literals for Unicode text.
252 However, you must use ``b"..."`` literals for binary data.
253
254* As the :class:`str` and :class:`bytes` types cannot be mixed, you
255 must always explicitly convert between them. Use :meth:`str.encode`
256 to go from :class:`str` to :class:`bytes`, and :meth:`bytes.decode`
257 to go from :class:`bytes` to :class:`str`. You can also use
258 ``bytes(s, encoding=...)`` and ``str(b, encoding=...)``,
259 respectively.
260
261* Like :class:`str`, the :class:`bytes` type is immutable. There is a
262 separate *mutable* type to hold buffered binary data,
263 :class:`bytearray`. Nearly all APIs that accept :class:`bytes` also
264 accept :class:`bytearray`. The mutable API is based on
265 :class:`collections.MutableSequence`.
266
267* All backslashes in raw string literals are interpreted literally.
268 This means that ``'\U'`` and ``'\u'`` escapes in raw strings are not
269 treated specially. For example, ``r'\u20ac'`` is a string of 6
270 characters in Python 3.0, whereas in 2.6, ``ur'\u20ac'`` was the
271 single "euro" character. (Of course, this change only affects raw
272 string literals; the euro character is ``'\u20ac'`` in Python 3.0.)
273
274* The builtin :class:`basestring` abstract type was removed. Use
275 :class:`str` instead. The :class:`str` and :class:`bytes` types
276 don't have functionality enough in common to warrant a shared base
277 class. The ``2to3`` tool (see below) replaces every occurrence of
278 :class:`basestring` with :class:`str`.
279
280* Files opened as text files (still the default mode for :func:`open`)
281 always use an encoding to map between strings (in memory) and bytes
282 (on disk). Binary files (opened with a ``b`` in the mode argument)
283 always use bytes in memory. This means that if a file is opened
284 using an incorrect mode or encoding, I/O will likely fail loudly,
285 instead of silently producing incorrect data. It also means that
286 even Unix users will have to specify the correct mode (text or
287 binary) when opening a file. There is a platform-dependent default
288 encoding, which on Unixy platforms can be set with the ``LANG``
289 environment variable (and sometimes also with some other
290 platform-specific locale-related environment variables). In many
291 cases, but not all, the system default is UTF-8; you should never
292 count on this default. Any application reading or writing more than
293 pure ASCII text should probably have a way to override the encoding.
294 There is no longer any need for using the encoding-aware streams
295 in the :mod:`codecs` module.
296
297* Filenames are passed to and returned from APIs as (Unicode) strings.
298 This can present platform-specific problems because on some
299 platforms filenames are arbitrary byte strings. (On the other hand,
300 on Windows filenames are natively stored as Unicode.) As a
301 work-around, most APIs (e.g. :func:`open` and many functions in the
302 :mod:`os` module) that take filenames accept :class:`bytes` objects
303 as well as strings, and a few APIs have a way to ask for a
304 :class:`bytes` return value. Thus, :func:`os.listdir` returns a
305 list of :class:`bytes` instances if the argument is a :class:`bytes`
306 instance, and :func:`os.getcwdu` returns the current working
307 directory as a :class:`bytes` instance. Note that when
308 :func:`os.listdir` returns a list of strings, filenames that
309 cannot be decoded properly are omitted rather than raising
310 :exc:`UnicodeError`.
311
312* Some system APIs like :data:`os.environ` and :data:`sys.argv` can
313 also present problems when the bytes made available by the system is
314 not interpretable using the default encoding. Setting the ``LANG``
315 variable and rerunning the program is probably the best approach.
316
317* :pep:`3138`: The :func:`repr` of a string no longer escapes
318 non-ASCII characters. It still escapes control characters and code
319 points with non-printable status in the Unicode standard, however.
320
321* :pep:`3120`: The default source encoding is now UTF-8.
322
323* :pep:`3131`: Non-ASCII letters are now allowed in identifiers.
324 (However, the standard library remains ASCII-only with the exception
325 of contributor names in comments.)
326
327* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead,
328 import the :mod:`io` module and use :class:`io.StringIO` or
329 :class:`io.BytesIO` for text and data respectively.
330
331* See also the :ref:`unicode-howto`, which was updated for Python 3.0.
332
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000333
Guido van Rossum73961352008-12-03 00:54:52 +0000334Overview Of Syntax Changes
335==========================
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000336
Guido van Rossum73961352008-12-03 00:54:52 +0000337This section gives a brief overview of every *syntactic* change in
338Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000339
Guido van Rossum38287682008-12-03 02:03:19 +0000340New Syntax
341----------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000342
Guido van Rossum73961352008-12-03 00:54:52 +0000343* :pep:`3107`: Function argument and return value annotations. This
344 provides a standardized way of annotating a function's parameters
345 and return value. There are no semantics attached to such
346 annotations except that they can be introspected at runtime using
347 the :attr:`__annotations__` attribute. The intent is to encourage
348 experimentation through metaclasses, decorators or frameworks.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000349
Guido van Rossum715287f2008-12-02 22:34:15 +0000350* :pep:`3102`: Keyword-only arguments. Named parameters occurring
351 after ``*args`` in the parameter list *must* be specified using
352 keyword syntax in the call. You can also use a bare ``*`` in the
353 parameter list to indicate that you don't accept a variable-length
354 argument list, but you do have keyword-only arguments.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000355
356* Keyword arguments are allowed after the list of base classes in a
357 class definition. This is used by the new convention for specifying
Guido van Rossumfedd1402008-12-03 04:15:35 +0000358 a metaclass (see next section), but can be used for other purposes
359 as well, as long as the metaclass supports it.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000360
Guido van Rossum715287f2008-12-02 22:34:15 +0000361* :pep:`3104`: :keyword:`nonlocal` statement. Using ``nonlocal x``
362 you can now assign directly to a variable in an outer (but
363 non-global) scope. :keyword:`nonlocal` is a new reserved word.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000364
Guido van Rossum715287f2008-12-02 22:34:15 +0000365* :pep:`3132`: Extended Iterable Unpacking. You can now write things
366 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
367 stuff``. The ``rest`` object is always a (possibly empty) list; the
368 right-hand side may be any iterable. Example::
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000369
Guido van Rossum715287f2008-12-02 22:34:15 +0000370 (a, *rest, b) = range(5)
371
372 This sets *a* to ``0``, *b* to ``4``, and \*rest to ``[1, 2, 3]``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000373
374* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
Guido van Rossum73961352008-12-03 00:54:52 +0000375 same thing as ``dict(stuff)`` but is more flexible. (This is
376 :pep:`0274` vindicated. :-)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000377
378* Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty
Guido van Rossum715287f2008-12-02 22:34:15 +0000379 dictionary; use ``set()`` for an empty set. Set comprehensions are
Guido van Rossum73961352008-12-03 00:54:52 +0000380 also supported; e.g., ``{x for x in stuff}`` means the same thing as
Guido van Rossum715287f2008-12-02 22:34:15 +0000381 ``set(stuff)`` but is more flexible.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000382
383* New octal literals, e.g. ``0o720`` (already in 2.6). The old octal
Walter Dörwaldeab34c92008-12-02 11:58:09 +0000384 literals (``0720``) are gone.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000385
Georg Brandla2361d92008-12-04 18:19:41 +0000386* New binary literals, e.g. ``0b1010`` (already in 2.6), and
387 there is a new corresponding builtin function, :func:`bin`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000388
Raymond Hettingerab4c51c2008-12-03 15:04:01 +0000389* Bytes literals are introduced with a leading ``b`` or ``B``, and
Georg Brandla2361d92008-12-04 18:19:41 +0000390 there is a new corresponding builtin function, :func:`bytes`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000391
Guido van Rossum38287682008-12-03 02:03:19 +0000392Changed Syntax
393--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000394
Guido van Rossumfedd1402008-12-03 04:15:35 +0000395* :pep:`3109` and :pep:`3134`: new :keyword:`raise` statement syntax:
396 ``raise [expr [from expr]]``. See below.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000397
Guido van Rossum715287f2008-12-02 22:34:15 +0000398* :keyword:`as` and :keyword:`with` are now reserved words. (Since
399 2.6, actually.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000400
Guido van Rossum715287f2008-12-02 22:34:15 +0000401* :keyword:`True`, :keyword:`False`, and :keyword:`None` are reserved
402 words. (2.6 partially enforced the restrictions on :keyword:`None`
403 already.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000404
Guido van Rossum715287f2008-12-02 22:34:15 +0000405* Change from :keyword:`except` *exc*, *var* to
406 :keyword:`except` *exc* :keyword:`as` *var*. See :pep:`3110`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000407
Guido van Rossumfedd1402008-12-03 04:15:35 +0000408* :pep:`3115`: New Metaclass Syntax. Instead of::
409
410 class C:
411 __metaclass__ = M
412 ...
413
414 you must now use::
415
416 class C(metaclass=M):
417 ...
418
419 The module-global :data:`__metaclass__` variable is no longer
420 supported. (It was a crutch to make it easier to default to
421 new-style classes without deriving every class from
422 :class:`object`.)
423
Guido van Rossum715287f2008-12-02 22:34:15 +0000424* List comprehensions no longer support the syntactic form
425 ``[... for var in item1, item2, ...]``. Use
426 ``[... for var in (item1, item2, ...)]`` instead.
427 Also note that list comprehensions have different semantics: they
428 are closer to syntactic sugar for a generator expression inside a
429 :func:`list` constructor, and in particular the loop control
430 variables are no longer leaked into the surrounding scope.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000431
Guido van Rossum715287f2008-12-02 22:34:15 +0000432* The *ellipsis* (``...``) can be used as an atomic expression
433 anywhere. (Previously it was only allowed in slices.) Also, it
434 *must* now be spelled as ``...``. (Previously it could also be
435 spelled as ``. . .``, by a mere accident of the grammar.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000436
Guido van Rossum38287682008-12-03 02:03:19 +0000437Removed Syntax
438--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000439
Guido van Rossum715287f2008-12-02 22:34:15 +0000440* :pep:`3113`: Tuple parameter unpacking removed. You can no longer
441 write ``def foo(a, (b, c)): ...``.
442 Use ``def foo(a, b_c): b, c = b_c`` instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000443
Guido van Rossum715287f2008-12-02 22:34:15 +0000444* Removed backticks (use :func:`repr` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000445
Guido van Rossum715287f2008-12-02 22:34:15 +0000446* Removed ``<>`` (use ``!=`` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000447
448* Removed keyword: :func:`exec` is no longer a keyword; it remains as
449 a function. (Fortunately the function syntax was also accepted in
Guido van Rossum715287f2008-12-02 22:34:15 +0000450 2.x.) Also note that :func:`exec` no longer takes a stream argument;
451 instead of ``exec(f)`` you can use ``exec(f.read())``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000452
453* Integer literals no longer support a trailing ``l`` or ``L``.
454
455* String literals no longer support a leading ``u`` or ``U``.
456
Guido van Rossum715287f2008-12-02 22:34:15 +0000457* The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only
458 allowed at the module level, no longer inside functions.
459
Andrew M. Kuchling2982c322008-12-04 15:07:14 +0000460* The only acceptable syntaxes for relative imports are :keyword:`from`
461 ``.`` :keyword:`import` *name* or
462 :keyword:`from` ``.module`` :keyword:`import` *name*.
463 :keyword:`import` forms not starting with ``.`` are always
464 interpreted as absolute imports. (:pep:`0328`)
Guido van Rossum715287f2008-12-02 22:34:15 +0000465
Guido van Rossumfedd1402008-12-03 04:15:35 +0000466* Classic classes are gone.
467
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000468
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000469Changes Already Present In Python 2.6
470=====================================
471
Guido van Rossum715287f2008-12-02 22:34:15 +0000472Since many users presumably make the jump straight from Python 2.5 to
473Python 3.0, this section reminds the reader of new features that were
474originally designed for Python 3.0 but that were back-ported to Python
4752.6. The corresponding sections in :ref:`whats-new-in-2.6` should be
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000476consulted for longer descriptions.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000477
Guido van Rossum715287f2008-12-02 22:34:15 +0000478* :ref:`pep-0343`. The :keyword:`with` statement is now a standard
Georg Brandlffd1a752008-12-03 06:44:59 +0000479 feature and no longer needs to be imported from the :mod:`__future__`.
Guido van Rossum715287f2008-12-02 22:34:15 +0000480 Also check out :ref:`new-26-context-managers` and
481 :ref:`new-module-contextlib`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000482
Guido van Rossum715287f2008-12-02 22:34:15 +0000483* :ref:`pep-0366`. This enhances the usefulness of the :option:`-m`
484 option when the referenced module lives in a package.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000485
Guido van Rossum715287f2008-12-02 22:34:15 +0000486* :ref:`pep-0370`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000487
Guido van Rossum715287f2008-12-02 22:34:15 +0000488* :ref:`pep-0371`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000489
Guido van Rossum715287f2008-12-02 22:34:15 +0000490* :ref:`pep-3101`. Note: the 2.6 description mentions the
491 :meth:`format` method for both 8-bit and Unicode strings. In 3.0,
492 only the :class:`str` type (text strings with Unicode support)
493 supports this method; the :class:`bytes` type does not. The plan is
494 to eventually make this the only API for string formatting, and to
495 start deprecating the ``%`` operator in Python 3.1.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000496
Guido van Rossum715287f2008-12-02 22:34:15 +0000497* :ref:`pep-3105`. This is now a standard feature and no longer needs
Guido van Rossum67d75ba2008-12-03 02:31:31 +0000498 to be imported from :mod:`__future__`. More details were given above.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000499
Guido van Rossum715287f2008-12-02 22:34:15 +0000500* :ref:`pep-3110`. The :keyword:`except` *exc* :keyword:`as` *var*
501 syntax is now standard and :keyword:`except` *exc*, *var* is no
502 longer supported. (Of course, the :keyword:`as` *var* part is still
503 optional.)
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000504
Guido van Rossum715287f2008-12-02 22:34:15 +0000505* :ref:`pep-3112`. The ``b"..."`` string literal notation (and its
506 variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now
Guido van Rossum38287682008-12-03 02:03:19 +0000507 produces a literal of type :class:`bytes`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000508
Guido van Rossum715287f2008-12-02 22:34:15 +0000509* :ref:`pep-3116`. The :mod:`io` module is now the standard way of
510 doing file I/O, and the initial values of :data:`sys.stdin`,
511 :data:`sys.stdout` and :data:`sys.stderr` are now instances of
512 :class:`io.TextIOBase`. The builtin :func:`open` function is now an
513 alias for :func:`io.open` and has additional keyword arguments
514 *encoding*, *errors*, *newline* and *closefd*. Also note that an
515 invalid *mode* argument now raises :exc:`ValueError`, not
Guido van Rossum38287682008-12-03 02:03:19 +0000516 :exc:`IOError`. The binary file object underlying a text file
517 object can be accessed as :attr:`f.buffer` (but beware that the
518 text object maintains a buffer of itself in order to speed up
519 the encoding and decoding operations).
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000520
Guido van Rossum715287f2008-12-02 22:34:15 +0000521* :ref:`pep-3118`. The old builtin :func:`buffer` is now really gone;
522 the new builtin :func:`memoryview` provides (mostly) similar
523 functionality.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000524
Guido van Rossum715287f2008-12-02 22:34:15 +0000525* :ref:`pep-3119`. The :mod:`abc` module and the ABCs defined in the
Guido van Rossum38287682008-12-03 02:03:19 +0000526 :mod:`collections` module plays a somewhat more prominent role in
Guido van Rossum715287f2008-12-02 22:34:15 +0000527 the language now, and builtin collection types like :class:`dict`
528 and :class:`list` conform to the :class:`collections.MutableMapping`
Raymond Hettingerab4c51c2008-12-03 15:04:01 +0000529 and :class:`collections.MutableSequence` ABCs, respectively.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000530
Guido van Rossum715287f2008-12-02 22:34:15 +0000531* :ref:`pep-3127`. As mentioned above, the new octal literal
532 notation is the only one supported, and binary literals have been
533 added.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000534
Guido van Rossum38287682008-12-03 02:03:19 +0000535* :ref:`pep-3129`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000536
Guido van Rossum715287f2008-12-02 22:34:15 +0000537* :ref:`pep-3141`. The :mod:`numbers` module is another new use of
538 ABCs, defining Python's "numeric tower". Also note the new
Guido van Rossum38287682008-12-03 02:03:19 +0000539 :mod:`fractions` module which implements :class:`numbers.Rational`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000540
541
542Library Changes
543===============
544
Guido van Rossumc46ee542008-12-02 23:46:46 +0000545Due to time constraints, this document does not exhaustively cover the
546very extensive changes to the standard library. :pep:`3108` is the
547reference for the major changes to the library. Here's a capsule
548review:
Guido van Rossum56076da2008-12-02 22:58:36 +0000549
Guido van Rossumc46ee542008-12-02 23:46:46 +0000550* Many old modules were removed. Some, like :mod:`gopherlib` (no
551 longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were
552 already deprecated by :pep:`0004`. Others were removed as a result
553 of the removal of support for various platforms such as Irix, BeOS
554 and Mac OS 9 (see :pep:`0011`). Some modules were also selected for
555 removal in Python 3.0 due to lack of use or because a better
556 replacement exists. See :pep:`3108` for an exhaustive list.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000557
Guido van Rossumc46ee542008-12-02 23:46:46 +0000558* The :mod:`bsddb3` package was removed because its presence in the
559 core standard library has proved over time to be a particular burden
Guido van Rossum08388ef2008-12-03 05:39:28 +0000560 for the core developers due to testing instability and Berkeley DB's
Guido van Rossumc46ee542008-12-02 23:46:46 +0000561 release schedule. However, the package is alive and well,
562 externally maintained at http://www.jcea.es/programacion/pybsddb.htm.
563
Andrew M. Kuchling2982c322008-12-04 15:07:14 +0000564* Some modules were renamed because their old name disobeyed
565 :pep:`0008`, or for various other reasons. Here's the list:
Guido van Rossumc46ee542008-12-02 23:46:46 +0000566
567 ======================= =======================
568 Old Name New Name
569 ======================= =======================
570 _winreg winreg
571 ConfigParser configparser
572 copy_reg copyreg
573 Queue queue
574 SocketServer socketserver
575 markupbase _markupbase
576 repr reprlib
577 test.test_support test.support
578 ======================= =======================
579
580* A common pattern in Python 2.x is to have one version of a module
581 implemented in pure Python, with an optional accelerated version
582 implemented as a C extension; for example, :mod:`pickle` and
583 :mod:`cPickle`. This places the burden of importing the accelerated
584 version and falling back on the pure Python version on each user of
585 these modules. In Python 3.0, the accelerated versions are
586 considered implementation details of the pure Python versions.
587 Users should always import the standard version, which attempts to
588 import the accelerated version and falls back to the pure Python
Guido van Rossumfedd1402008-12-03 04:15:35 +0000589 version. The :mod:`pickle` / :mod:`cPickle` pair received this
590 treatment. The :mod:`profile` module is on the list for 3.1. The
591 :mod:`StringIO` module has been turned into a class in the :mod:`io`
592 module.
Guido van Rossumc46ee542008-12-02 23:46:46 +0000593
594* Some related modules have been grouped into packages, and usually
595 the submodule names have been simplified. The resulting new
596 packages are:
597
598 * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`,
599 :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`).
600
601 * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`).
602
603 * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`,
604 :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`,
605 :mod:`cookielib`).
606
607 * :mod:`tkinter` (all :mod:`Tkinter`-related modules except
608 :mod:`turtle`). The target audience of :mod:`turtle` doesn't
609 really care about :mod:`tkinter`. Also note that as of Python
610 2.6, the functionality of :mod:`turtle` has been greatly enhanced.
611
Andrew M. Kuchling2982c322008-12-04 15:07:14 +0000612 * :mod:`urllib` (:mod:`urllib`, :mod:`urllib2`, :mod:`urlparse`,
Guido van Rossumc46ee542008-12-02 23:46:46 +0000613 :mod:`robotparse`).
614
615 * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`,
616 :mod:`SimpleXMLRPCServer`).
Christian Heimes6e72b9e2008-12-03 13:39:03 +0000617
Guido van Rossum08388ef2008-12-03 05:39:28 +0000618Some other changes to standard library modules, not covered by
Guido van Rossumfedd1402008-12-03 04:15:35 +0000619:pep:`3108`:
Guido van Rossum715287f2008-12-02 22:34:15 +0000620
621* Killed :mod:`sets`. Use the builtin :func:`set` function.
622
Guido van Rossumc46ee542008-12-02 23:46:46 +0000623* Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`,
624 :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`,
625 :data:`sys.exc_traceback`. (Note that :data:`sys.last_type`
Guido van Rossum715287f2008-12-02 22:34:15 +0000626 etc. remain.)
627
Guido van Rossumc46ee542008-12-02 23:46:46 +0000628* Cleanup of the :class:`array.array` type: the :meth:`read` and
629 :meth:`write` methods are gone; use :meth:`fromfile` and
630 :meth:`tofile` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000631
Guido van Rossumc46ee542008-12-02 23:46:46 +0000632* Cleanup of the :mod:`operator` module: removed
633 :func:`sequenceIncludes` and :func:`isCallable`.
Guido van Rossum715287f2008-12-02 22:34:15 +0000634
Guido van Rossumc46ee542008-12-02 23:46:46 +0000635* Cleanup of the :mod:`thread` module: :func:`acquire_lock` and
636 :func:`release_lock` are gone; use :func:`acquire` and
637 :func:`release` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000638
Guido van Rossumc46ee542008-12-02 23:46:46 +0000639* Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API.
Guido van Rossum715287f2008-12-02 22:34:15 +0000640
Guido van Rossumfedd1402008-12-03 04:15:35 +0000641* The :mod:`new` module is gone.
642
643* The functions :func:`os.tmpnam`, :func:`os.tempnam` and
644 :func:`os.tmpfile` have been removed in favor of the :mod:`tempfile`
645 module.
646
647* The :mod:`tokenize` module has been changed to work with bytes. The
648 main entry point is now :func:`tokenize.tokenize`, instead of
649 generate_tokens.
650
651* :data:`string.letters` and its friends (:data:`string.lowercase` and
652 :data:`string.uppercase`) are gone. Use
653 :data:`string.ascii_letters` etc. instead. (The reason for the
Andrew M. Kuchling2982c322008-12-04 15:07:14 +0000654 removal is that :data:`string.letters` and friends had
Guido van Rossumfedd1402008-12-03 04:15:35 +0000655 locale-specific behavior, which is a bad idea for such
656 attractively-named global "constants".)
657
658* Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the
659 underscores, adding an 's'). The :data:`__builtins__` variable
660 found in most global namespaces is unchanged. To modify a builtin,
661 you should use :mod:`builtins`, not :data:`__builtins__`!
662
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000663
Guido van Rossum715287f2008-12-02 22:34:15 +0000664:pep:`3101`: A New Approach To String Formatting
665================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000666
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000667* A new system for built-in string formatting operations replaces the
668 ``%`` string formatting operator. (However, the ``%`` operator is
669 still supported; it will be deprecated in Python 3.1 and removed
Guido van Rossum38287682008-12-03 02:03:19 +0000670 from the language at some later time.) Read :pep:`3101` for the full
671 scoop.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000672
673
Guido van Rossumfedd1402008-12-03 04:15:35 +0000674Changes To Exceptions
675=====================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000676
Guido van Rossumfedd1402008-12-03 04:15:35 +0000677The APIs for raising and catching exception have been cleaned up and
678new powerful features added:
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000679
Guido van Rossum715287f2008-12-02 22:34:15 +0000680* :pep:`0352`: All exceptions must be derived (directly or indirectly)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000681 from :exc:`BaseException`. This is the root of the exception
Guido van Rossumfedd1402008-12-03 04:15:35 +0000682 hierarchy. This is not new as a recommendation, but the
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000683 *requirement* to inherit from :exc:`BaseException` is new. (Python
684 2.6 still allowed classic classes to be raised, and placed no
Guido van Rossumfedd1402008-12-03 04:15:35 +0000685 restriction on what you can catch.) As a consequence, string
686 exceptions are finally truly and utterly dead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000687
Guido van Rossumfedd1402008-12-03 04:15:35 +0000688* Almost all exceptions should actually derive from :exc:`Exception`;
689 :exc:`BaseException` should only be used as a base class for
690 exceptions that should only be handled at the top level, such as
691 :exc:`SystemExit` or :exc:`KeyboardInterrupt`. The recommended
692 idiom for handling all exceptions except for this latter category is
693 to use :keyword:`except` :exc:`Exception`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000694
Guido van Rossumfedd1402008-12-03 04:15:35 +0000695* :exc:`StandardError` was removed (in 2.6 already).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000696
Guido van Rossumfedd1402008-12-03 04:15:35 +0000697* Exceptions no longer behave as sequences. Use the :attr:`args`
698 attribute instead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000699
Guido van Rossumfedd1402008-12-03 04:15:35 +0000700* :pep:`3109`: Raising exceptions. You must now use :keyword:`raise`
701 *Exception*(*args*) instead of :keyword:`raise` *Exception*, *args*.
702 Additionally, you can no longer explicitly specify a traceback;
703 instead, if you *have* to do this, you can assign directly to the
704 :attr:`__traceback__` attribute (see below).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000705
Guido van Rossumfedd1402008-12-03 04:15:35 +0000706* :pep:`3110`: Catching exceptions. You must now use
Andrew M. Kuchling2982c322008-12-04 15:07:14 +0000707 :keyword:`except` *SomeException* :keyword:`as` *variable* instead
Fred Drake9baee312008-12-04 15:28:51 +0000708 of :keyword:`except` *SomeException*, *variable*. Moreover, the
Guido van Rossumfedd1402008-12-03 04:15:35 +0000709 *variable* is explicitly deleted when the :keyword:`except` block
710 is left.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000711
Guido van Rossumfedd1402008-12-03 04:15:35 +0000712* :pep:`3134`: Exception chaining. There are two cases: implicit
713 chaining and explicit chaining. Implicit chaining happens when an
714 exception is raised in an :keyword:`except` or :keyword:`finally`
715 handler block. This usually happens due to a bug in the handler
716 block; we call this a *secondary* exception. In this case, the
717 original exception (that was being handled) is saved as the
718 :attr:`__context__` attribute of the secondary exception.
719 Explicit chaining is invoked with this syntax::
720
721 raise SecondaryException() from primary_exception
722
723 (where *primary_exception* is any expression that produces an
724 exception object, probably an exception that was previously caught).
725 In this case, the primary exception is stored on the
726 :attr:`__cause__` attribute of the secondary exception. The
727 traceback printed when an unhandled exception occurs walks the chain
728 of :attr:`__cause__` and :attr:`__context__` attributes and prints a
729 separate traceback for each component of the chain, with the primary
Fred Drake9baee312008-12-04 15:28:51 +0000730 exception at the top. (Java users may recognize this behavior.)
Guido van Rossumfedd1402008-12-03 04:15:35 +0000731
732* :pep:`3134`: Exception objects now store their traceback as the
733 :attr:`__traceback__` attribute. This means that an exception
734 object now contains all the information pertaining to an exception,
735 and there are fewer reasons to use :func:`sys.exc_info` (though the
736 latter is not removed).
737
738* A few exception messages are improved when Windows fails to load an
739 extension module. For example, ``error code 193`` is now ``%1 is
740 not a valid Win32 application``. Strings now deal with non-English
741 locales.
Georg Brandl396ef802008-02-02 10:30:18 +0000742
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000743
Guido van Rossumfedd1402008-12-03 04:15:35 +0000744Miscellaneous Other Changes
745===========================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000746
Guido van Rossumfedd1402008-12-03 04:15:35 +0000747Operators And Special Methods
748-----------------------------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000749
Georg Brandl396ef802008-02-02 10:30:18 +0000750* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
Guido van Rossumfedd1402008-12-03 04:15:35 +0000751 :data:`NotImplemented`.
Georg Brandl396ef802008-02-02 10:30:18 +0000752
Guido van Rossumfedd1402008-12-03 04:15:35 +0000753* The concept of "unbound methods" has been removed from the language.
Guido van Rossum715287f2008-12-02 22:34:15 +0000754 When referencing a method as a class attribute, you now get a plain
755 function object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000756
Guido van Rossum715287f2008-12-02 22:34:15 +0000757* :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__`
758 were killed. The syntax ``a[i:j]`` now translates to
759 ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
760 :meth:`__delitem__`, when used as an assignment or deletion target,
761 respectively).
Georg Brandl396ef802008-02-02 10:30:18 +0000762
Guido van Rossumfedd1402008-12-03 04:15:35 +0000763* :pep:`3114`: the standard :meth:`next` method has been renamed to
764 :meth:`__next__`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000765
Guido van Rossumfedd1402008-12-03 04:15:35 +0000766* The :meth:`__oct__` and :meth:`__hex__` special methods are removed
767 -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert
768 the argument to an integer.
769
770* Removed support for :attr:`__members__` and :attr:`__methods__`.
771
772* The function attributes named :attr:`func_X` have been renamed to
773 use the :data:`__X__` form, freeing up these names in the function
774 attribute namespace for user-defined attributes. To wit,
775 :attr:`func_closure`, :attr:`func_code`, :attr:`func_defaults`,
776 :attr:`func_dict`, :attr:`func_doc`, :attr:`func_globals`,
777 :attr:`func_name` were renamed to :attr:`__closure__`,
778 :attr:`__code__`, :attr:`__defaults__`, :attr:`__dict__`,
779 :attr:`__doc__`, :attr:`__globals__`, :attr:`__name__`,
780 respectively.
781
782* :meth:`__nonzero__` is now :meth:`__bool__`.
783
784Builtins
785--------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000786
Guido van Rossum715287f2008-12-02 22:34:15 +0000787* :pep:`3135`: New :func:`super`. You can now invoke :func:`super`
Guido van Rossumfedd1402008-12-03 04:15:35 +0000788 without arguments and (assuming this is in a regular instance method
789 defined inside a :keyword:`class` statement) the right class and
790 instance will automatically be chosen. With arguments, the behavior
791 of :func:`super` is unchanged.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000792
Guido van Rossumfedd1402008-12-03 04:15:35 +0000793* :pep:`3111`: :func:`raw_input` was renamed to :func:`input`. That
794 is, the new :func:`input` function reads a line from
795 :data:`sys.stdin` and returns it with the trailing newline stripped.
796 It raises :exc:`EOFError` if the input is terminated prematurely.
797 To get the old behavior of :func:`input`, use ``eval(input())``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000798
Guido van Rossumfedd1402008-12-03 04:15:35 +0000799* A new builtin :func:`next` was added to call the :meth:`__next__`
800 method on an object.
801
802* Moved :func:`intern` to :func:`sys.intern`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000803
Guido van Rossum715287f2008-12-02 22:34:15 +0000804* Removed: :func:`apply`. Instead of ``apply(f, args)`` use
805 ``f(*args)``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000806
Guido van Rossum715287f2008-12-02 22:34:15 +0000807* Removed :func:`callable`. Instead of ``callable(f)`` you can use
808 ``hasattr(f, '__call__')``. The :func:`operator.isCallable` function
809 is also gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000810
Guido van Rossum715287f2008-12-02 22:34:15 +0000811* Removed :func:`coerce`. This function no longer serves a purpose
812 now that classic classes are gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000813
Guido van Rossum715287f2008-12-02 22:34:15 +0000814* Removed :func:`execfile`. Instead of ``execfile(fn)`` use
815 ``exec(open(fn).read())``.
Georg Brandl396ef802008-02-02 10:30:18 +0000816
Guido van Rossum715287f2008-12-02 22:34:15 +0000817* Removed :class:`file`. Use :func:`open`.
Georg Brandl396ef802008-02-02 10:30:18 +0000818
Guido van Rossum715287f2008-12-02 22:34:15 +0000819* Removed :func:`reduce`. Use :func:`functools.reduce` if you really
820 need it; however, 99 percent of the time an explicit :keyword:`for`
821 loop is more readable.
Georg Brandl396ef802008-02-02 10:30:18 +0000822
Guido van Rossum715287f2008-12-02 22:34:15 +0000823* Removed :func:`reload`. Use :func:`imp.reload`.
824
825* Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator
826 instead.
827
Guido van Rossumfedd1402008-12-03 04:15:35 +0000828.. ======================================================================
Guido van Rossum715287f2008-12-02 22:34:15 +0000829
Guido van Rossumfedd1402008-12-03 04:15:35 +0000830
831Build and C API Changes
832=======================
833
834Due to time constraints, here is a *very* incomplete list of changes
835to the C API.
836
837* Support for several platforms was dropped, including but not limited
838 to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.
839
840* :pep:`3118`: New Buffer API.
841
842* :pep:`3121`: Extension Module Initialization & Finalization.
843
844* :pep:`3123`: Making :cmacro:`PyObject_HEAD` conform to standard C.
845
846* No more C API support for restricted execution.
847
848* :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`,
849 :cfunc:`PyMember_Get`, and :cfunc:`PyMember_Set` C APIs are removed.
850
851* New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like
852 :cfunc:`PyImport_ImportModule` but won't block on the import lock
853 (returning an error instead).
Guido van Rossum715287f2008-12-02 22:34:15 +0000854
855* Renamed the boolean conversion C-level slot and method:
Guido van Rossumfedd1402008-12-03 04:15:35 +0000856 ``nb_nonzero`` is now ``nb_bool``.
Guido van Rossum715287f2008-12-02 22:34:15 +0000857
Guido van Rossum08388ef2008-12-03 05:39:28 +0000858* Removed :cmacro:`METH_OLDARGS` and :cmacro:`WITH_CYCLE_GC` from the C API.
Georg Brandl116aa622007-08-15 14:28:22 +0000859
Georg Brandl5a165582007-08-31 06:15:01 +0000860.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000861
862
Guido van Rossumfedd1402008-12-03 04:15:35 +0000863Performance
864===========
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000866The net result of the 3.0 generalizations is that Python 3.0 runs the
Guido van Rossum715287f2008-12-02 22:34:15 +0000867pystone benchmark around 10% slower than Python 2.5. Most likely the
868biggest cause is the removal of special-casing for small integers.
869There's room for improvement, but it will happen after 3.0 is
870released!
Georg Brandl116aa622007-08-15 14:28:22 +0000871
Georg Brandl5a165582007-08-31 06:15:01 +0000872.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000873
874
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000875Porting To Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +0000876=====================
877
Guido van Rossum56076da2008-12-02 22:58:36 +0000878For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
879best strategy is the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000880
Guido van Rossum56076da2008-12-02 22:58:36 +00008810. (Prerequisite:) Start with excellent test coverage.
Georg Brandl116aa622007-08-15 14:28:22 +0000882
Guido van Rossum56076da2008-12-02 22:58:36 +00008831. Port to Python 2.6. This should be no more work than the average
884 port from Python 2.x to Python 2.(x+1). Make sure all your tests
885 pass.
Christian Heimesf78b1c62007-12-02 16:52:32 +0000886
Guido van Rossum56076da2008-12-02 22:58:36 +00008872. (Still using 2.6:) Turn on the :option:`-3` command line switch.
888 This enables warnings about features that will be removed (or
889 change) in 3.0. Run your test suite again, and fix code that you
890 get warnings about until there are no warnings left, and all your
891 tests still pass.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000892
Guido van Rossum56076da2008-12-02 22:58:36 +00008933. Run the ``2to3`` source-to-source translator over your source code
894 tree. (See :ref:`2to3-reference` for more on this tool.) Run the
895 result of the translation under Python 3.0. Manually fix up any
896 remaining issues, fixing problems until all tests pass again.
897
898It is not recommended to try to write source code that runs unchanged
899under both Python 2.6 and 3.0; you'd have to use a very contorted
900coding style, e.g. avoiding :keyword:`print` statements, metaclasses,
901and much more. If you are maintaining a library that needs to support
902both Python 2.6 and Python 3.0, the best approach is to modify step 3
903above by editing the 2.6 version of the source code and running the
904``2to3`` translator again, rather than editing the 3.0 version of the
905source code.
906
907For porting C extensions to Python 3.0, please see :ref:`cporting-howto`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000908
Georg Brandl5a165582007-08-31 06:15:01 +0000909.. ======================================================================