blob: d5414dd65deae73a18fefe1d9b9f0593c46b8a41 [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 Rossum4a98a2a2008-11-21 18:35:43 +00005.. XXX add trademark info for Apple, Microsoft, SourceForge.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Guido van Rossumeb3d8d42008-12-02 00:56:25 +00007.. XXX turn all PEP references into :pep:`NNN` markup.
8
Guido van Rossum4a98a2a2008-11-21 18:35:43 +00009:Author: Guido van Rossum
10:Release: |release|
11:Date: |today|
12
13.. $Id$
14 Rules for maintenance:
Georg Brandl5a165582007-08-31 06:15:01 +000015
16 * Anyone can add text to this document. Do not spend very much time
17 on the wording of your changes, because your text will probably
18 get rewritten to some degree.
19
20 * The maintainer will go through Misc/NEWS periodically and add
21 changes; it's therefore more important to add your changes to
22 Misc/NEWS than to this file.
23
24 * This is not a complete list of every single change; completeness
25 is the purpose of Misc/NEWS. Some changes I consider too small
26 or esoteric to include. If such a change is added to the text,
27 I'll just remove it. (This is another reason you shouldn't spend
28 too much time on writing your addition.)
29
30 * If you want to draw your new text to the attention of the
31 maintainer, add 'XXX' to the beginning of the paragraph or
32 section.
33
34 * It's OK to just add a fragmentary note about a change. For
35 example: "XXX Describe the transmogrify() function added to the
36 socket module." The maintainer will research the change and
37 write the necessary text.
38
39 * You can comment out your additions if you like, but it's not
40 necessary (especially when a final release is some months away).
41
42 * Credit the author of a patch or bugfix. Just the name is
43 sufficient; the e-mail address isn't necessary.
44
45 * It's helpful to add the bug/patch number as a comment:
46
47 % Patch 12345
48 XXX Describe the transmogrify() function added to the socket
49 module.
50 (Contributed by P.Y. Developer.)
51
52 This saves the maintainer the effort of going through the SVN log
53 when researching a change.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000055This article explains the new features in Python 3.0, compared to 2.6.
56Python 3.0 is the first ever *intentionally incompatible* release.
57There are more changes than in a typical release, and more that are
58important for all Python users. Nevertheless, after digesting the
59changes, you'll find that Python really hasn't changed all that much
60-- by and large, we're merely fixing well-known annoyances and warts.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000061
62This article doesn't attempt to provide a complete specification of
63the new features, but instead provides a convenient overview. For
64full details, you should refer to the documentation for Python 3.0. If
65you want to understand the complete implementation and design
66rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000067
Georg Brandl5a165582007-08-31 06:15:01 +000068.. Compare with previous release in 2 - 3 sentences here.
69.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000070
Georg Brandl5a165582007-08-31 06:15:01 +000071.. ======================================================================
72.. Large, PEP-level features and changes should be described here.
73.. Should there be a new section here for 3k migration?
74.. Or perhaps a more general section describing module changes/deprecation?
75.. sets module deprecated
76.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000077
78
Guido van Rossumb197f3c2007-08-31 00:37:00 +000079Common Stumbling Blocks
80=======================
81
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000082This section briefly lists a few changes that are more likely to trip
83people up, without necessarily raising obvious errors. Most issues
84are explained in more detail in later sections.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000085
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000086Print Is A Function
87-------------------
Guido van Rossumdff1c312007-09-06 14:46:41 +000088
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000089The ``print`` statement has been replaced with a :func:`print` function,
90with keyword arguments to replace most of the special syntax of the
91old ``print`` statement (PEP 3105). Examples::
Guido van Rossumdff1c312007-09-06 14:46:41 +000092
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000093 Old: print "The answer is", 2*2
94 New: print("The answer is", 2*2)
Guido van Rossumdff1c312007-09-06 14:46:41 +000095
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000096 Old: print x, # Trailing comma suppresses newline
97 New: print(x, end=" ") # Appends a space instead of a newline
Guido van Rossumdff1c312007-09-06 14:46:41 +000098
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000099 Old: print # Prints a newline
100 New: print() # You must call the function!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000101
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000102 Old: print >>sys.stderr, "fatal error"
103 New: print("fatal error", file=sys.stderr)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000104
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000105 Old: print (x, y) # prints repr((x, y))
106 New: print((x, y)) # Not the same as print(x, y)!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000107
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000108You can also customize the separator between items, e.g.::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000109
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000110 print("There are <", 2**32, "> possibilities!", sep="")
Guido van Rossumdff1c312007-09-06 14:46:41 +0000111
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000112which produces::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000113
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000114 There are <4294967296> possibilities!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000115
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000116Note:
Guido van Rossumdff1c312007-09-06 14:46:41 +0000117
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000118* The :func:`print` function doesn't support the "softspace" feature of
119 the old ``print`` statement. For example, in Python 2.x,
120 ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
121 ``print("A\n", "B")`` writes ``"A\n B\n"``.
Guido van Rossumdff1c312007-09-06 14:46:41 +0000122
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000123* Initially, you'll be finding yourself typing the old ``print x``
124 a lot in interactive mode. Time to retrain your fingers to type
125 ``print(x)`` instead!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000126
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000127* When using the ``2to3`` source-to-source conversion tool, all
128 ``print`` statements are automatically converted to :func:`print`
129 function calls, so this is mostly a non-issue for larger projects.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000130
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000131Text Strings Vs. Bytes
132----------------------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000133
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000134Everything you thought you knew about binary data and Unicode has
135changed:
136
137* Python 3.0 uses *strings* and *bytes* instead of *Unicode strings*
138 and *8-bit strings*. The difference is that any attempt to mix
139 strings and bytes in Python 3.0 raises a TypeError exception,
140 whereas if you were to mix Unicode and 8-bit strings in Python 2.x,
141 you would only get an exception if the 8-bit string contained
142 non-ASCII values. As a consequence, pretty much all code that
143 uses Unicode, encodings or binary data most likely has to change.
144 The change is for the better, as in the 2.x world there were
145 numerous bugs having to do with mixing encoded and unencoded text.
146
147* Files opened as text files (still the default mode for :func:`open`)
148 always use an encoding to map between strings (in memory) and bytes
149 (on disk). Binary files (opened with a ``b`` in the mode argument)
150 always use bytes in memory. This means that if a file is opened
151 using an incorrect mode or encoding, I/O will likely fail. There is
152 a platform-dependent default encoding, which on Unixy platforms can
153 be set with the ``LANG`` environment variable (and sometimes also
154 with some other platform-specific locale-related environment
155 variables). In many cases, but not all, the system default is
156 UTF-8; you should never could on this default. Any application
157 reading or writing more than pure ASCII text should probably have a
158 way to override the encoding.
159
160* XXX More below?
161
162* See also the *Unicode HOWTO*. (XXX How to make this a link?)
163 (XXX Move to longer section below?)
164
165Views And Interators Instead Of Lists
166-------------------------------------
167
168Some well-known APIs no longer return lists:
169
170* :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and
171 :meth:`dict.values` return "views" instead of lists. For example,
172 this no longer works: ``k = d.keys(); k.sort()``. Use ``k =
173 sorted(d)`` instead.
174
175* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
176 :meth:`dict.itervalues` methods are no longer supported.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000177
Georg Brandlec17d202008-02-02 10:44:37 +0000178* :func:`map` and :func:`filter` return iterators. A quick fix is e.g.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000179 ``list(map(...))``, but a better fix is often to use a list
Georg Brandlec17d202008-02-02 10:44:37 +0000180 comprehension (especially when the original code uses :keyword:`lambda`).
181 Particularly tricky is :func:`map` invoked for the side effects of the
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000182 function; the correct transformation is to use a for-loop.
183
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000184* :func:`range` now behaves like :func:`xrange` used to behave.
185 The latter no longer exists.
186
187* :func:`zip` now returns an iterator.
188
189* XXX More below?
190
191Ordering Comparisons
192--------------------
193
194Python 3.0 has simplified the rules for ordering comparisons:
195
196* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
197 raise a TypeError exception when the operands don't have a
198 meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0
199 > None`` or ``len <= len`` are no longer valid. A corollary is that
200 sorting a heterogeneous list no longer makes sense -- all the
201 elements must be comparable to each other. Note that this does not
202 apply to the ``==`` and ``!=`` operators: objects of different
203 uncomparable types always compare unequal to each other, and an
204 object always compares equal to itself (i.e., ``x is y`` implies ``x
205 = y``; this is true even for ``NaN``).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000206
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000207* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the *cmp*
Kurt B. Kaiser9d0d6162008-02-14 02:47:50 +0000208 argument providing a comparison function. Use the *key* argument
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000209 instead. N.B. the *key* and *reverse* arguments are now "keyword-only".
Kurt B. Kaisera1401012008-02-13 16:09:27 +0000210
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000211* The :func:`cmp` function is gone, and the :meth:`__cmp__` special
212 method is no longer supported. Use :meth:`__lt__` for sorting,
213 :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as
214 needed. if you really need the :func:`cmp` functionality, the
215 expression ``(a > b) - (a < b)`` is equivalent to ``cmp(a, b)``.
216
217* XXX More below?
218
219Integers
220--------
221
222* We unified the :class:`int` and :class:`long` types. All integers
223 are now of type :class:`int`.
Georg Brandlb6670872008-11-22 08:35:59 +0000224
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000225* ``1/2`` returns a float. Use ``1//2`` to get the truncating behavior.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000226 (The latter syntax has existed for years, at least since Python 2.2.)
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000227
Georg Brandlec17d202008-02-02 10:44:37 +0000228* The :func:`repr` of a long integer doesn't include the trailing ``L``
Georg Brandlcc595bd2007-12-09 09:04:01 +0000229 anymore, so code that unconditionally strips that character will
230 chop off the last digit instead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000231
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000232* The :data:`sys.maxint` constant was removed, since there is no
233 longer a limit to the value of ints. However, :data:`sys.maxsize`
234 can be used as an integer larger than any practical list or string
235 index. It conforms to the implementation's "natural" integer size
236 and is typically the same as :data:`sys.maxint` in previous releases
237 on the same platform (assuming the same build options).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000238
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000239* XXX More below?
240
241
242Overview Of Syntactic Changes
243=============================
244
245This section gives a brief overview of every *syntactic* change.
246Several of these are discussed at greater length later.
247
248XXX Did I get everything?
249
250Additions
251---------
252
253* Function argument and return value annotations (see below). XXX
254
255* A lone ``*`` in a formal parameter list implies that any following
256 arguments *must* be specified in keyword form. (XXX Didn't this make
257 it into 2.6 as well?)
258
259* Keyword arguments are allowed after the list of base classes in a
260 class definition. This is used by the new convention for specifying
261 a metaclass, but can be used for other purposes as well, as long as
262 the metaclass supports it.
263
264* Tuple-unpacking assignment now has a *wildcard* syntax, e.g.::
265
266 (a, b, *rest) = range(5)
267
268 This sets *a* to 0, *b* to 1, and *rest to ``[2, 3, 4]``.
269
270* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
271 same thing as ``dict(stuff)`` but is more flexible.
272
273* Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty
274 dictionary; use ``set()`` for an empty set. Set comprehensions
275 are also supported; ``{x for x in stuff}`` means the same thing
276 as ``set(stuff)`` but is more flexible.
277
278* New octal literals, e.g. ``0o720`` (already in 2.6). The old octal
279 literals (``0720`` are gone.
280
281* New binary literals, e.g. ``0b1010`` (already in 2.6).
282
283* Bytes literals are introduced with a leading ``b`` or ``B``.
284
285Changes
286-------
287
288* New :keyword:`raise` statement syntax: ``raise [expr [from expr]]``.
289
290* New keywords: :keyword:`as`, :keyword:`with` (already in 2.6),
291 :keyword:`None` (partially enforced in 2.6), :keyword:`True`,
292 :keyword:`False` (these were built-ins previously), and
293 :keyword:`nonlocal` (for the new ``nonlocal`` statement).
294
295* Change from ``except exc, var:`` to ``except exc as var:``. XXX
296
297* *Very* subtle changes in the syntax for list comprehensions,
298 generator expressions, :keyword:`lambda expression and :keyword:`if`
299 expressions. For example, this is valid in Python 2.6::
300
301 [ x for x in lambda: True, lambda: False if x() ]
302
303 In Python 3.0 you'll have to add parentheses, like this::
304
305 [ x for x in (lambda: True, lambda: False) if x() ]
306
307* The *ellipsis* (``...``) can be used as an atomic expression anywhere.
308 (Previously it was only allowed in slices.)
309
310Removals
311--------
312
313* Tuple parameter unpacking removed. XXX
314
315* Removal of backticks. XXX
316
317* Removal of ``<>``. Use ``!=`` instead. XXX
318
319* Removed keyword: :func:`exec` is no longer a keyword; it remains as
320 a function. (Fortunately the function syntax was also accepted in
321 2.x.)
322
323* Integer literals no longer support a trailing ``l`` or ``L``.
324
325* String literals no longer support a leading ``u`` or ``U``.
326
327* The *ellipsis* must now be spelled as ``...``; previously it could
328 (by a mere accident of the grammar) also be spelled as ``. . .``.
329
330
331Changes Already Present In Python 2.6
332=====================================
333
334This section reminds the reader of new features that were originally
335designed for Python 3.0 but that were already introduced in Python
3362.6. The descriptions in "What's New in Python 2.6" are hereby
337included by reference.
338
339* XXX List each of those briefly.
340
341Strings And Bytes
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000342=================
343
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000344This section discusses the many changes in string
345
Georg Brandlec17d202008-02-02 10:44:37 +0000346* There is only one string type; its name is :class:`str` but its behavior and
347 implementation are like :class:`unicode` in 2.x.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000348
Georg Brandlec17d202008-02-02 10:44:37 +0000349* The :class:`basestring` superclass has been removed. The ``2to3`` tool
Raymond Hettinger6a883842008-07-22 19:27:12 +0000350 replaces every occurrence of :class:`basestring` with :class:`str`.
Christian Heimesf534f7b2008-01-25 11:02:28 +0000351
Georg Brandlec17d202008-02-02 10:44:37 +0000352* PEP 3137: There is a new type, :class:`bytes`, to represent binary data (and
353 encoded text, which is treated as binary data until you decide to decode it).
354 The :class:`str` and :class:`bytes` types cannot be mixed; you must always
355 explicitly convert between them, using the :meth:`str.encode` (str -> bytes)
356 or :meth:`bytes.decode` (bytes -> str) methods.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000357
Benjamin Petersona2f837f2008-04-28 21:05:10 +0000358* All backslashes in raw strings are interpreted literally. This means that
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000359 ``'\U'`` and ``'\u'`` escapes in raw strings are not treated specially.
Benjamin Petersona2f837f2008-04-28 21:05:10 +0000360
Georg Brandlec17d202008-02-02 10:44:37 +0000361.. XXX add bytearray
362
363* PEP 3112: Bytes literals, e.g. ``b"abc"``, create :class:`bytes` instances.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000364
365* PEP 3120: UTF-8 default source encoding.
366
Georg Brandlec17d202008-02-02 10:44:37 +0000367* PEP 3131: Non-ASCII identifiers. (However, the standard library remains
368 ASCII-only with the exception of contributor names in comments.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000369
370* PEP 3116: New I/O Implementation. The API is nearly 100% backwards
Georg Brandlec17d202008-02-02 10:44:37 +0000371 compatible, but completely reimplemented (currently mostly in Python). Also,
372 binary files use bytes instead of strings.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000373
Georg Brandlec17d202008-02-02 10:44:37 +0000374* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead, import
375 :class:`io.StringIO` or :class:`io.BytesIO`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000376
Benjamin Petersona2f837f2008-04-28 21:05:10 +0000377
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000378
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000379PEP 3101: A New Approach To String Formatting
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000380=============================================
381
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000382* A new system for built-in string formatting operations replaces the
383 ``%`` string formatting operator. (However, the ``%`` operator is
384 still supported; it will be deprecated in Python 3.1 and removed
385 from the language at some later time.)
Georg Brandl396ef802008-02-02 10:30:18 +0000386
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000387.. XXX expand this
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000388
389
Georg Brandlec17d202008-02-02 10:44:37 +0000390PEP 3106: Revamping dict :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values`
391======================================================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000392
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000393.. XXX expand this (but note that the "pitfalls" section currently has
394.. XXX more detail :-)
Georg Brandl396ef802008-02-02 10:30:18 +0000395
Georg Brandlec17d202008-02-02 10:44:37 +0000396* The :meth:`dict.iterkeys`, :meth:`dict.itervalues` and :meth:`dict.iteritems`
397 methods have been removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000398
Georg Brandlec17d202008-02-02 10:44:37 +0000399* :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` return objects
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000400 with set behavior that reference the underlying dict; these are often
401 referred to as *dictionary views*.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000402
403
404PEP 3107: Function Annotations
405==============================
406
Georg Brandl396ef802008-02-02 10:30:18 +0000407.. XXX expand this
408
Georg Brandlec17d202008-02-02 10:44:37 +0000409* A standardized way of annotating a function's parameters and return values.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000410
411
412Exception Stuff
413===============
414
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000415* PEP 352: All exceptions must be derived (directly or indirectly)
416 from :exc:`BaseException`. This is the root of the exception
417 hierarchy. Most exceptions should actually be derived from
418 :exc:`Exception`. This is not a new recommendation, but the
419 *requirement* to inherit from :exc:`BaseException` is new. (Python
420 2.6 still allowed classic classes to be raised, and placed no
421 restriction on what you can catch.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000422
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000423* :exc:`StandardError` was removed (in 2.6, actually).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000424
Georg Brandlec17d202008-02-02 10:44:37 +0000425* Dropping sequence behavior (slicing!) and :attr:`message` attribute of
Georg Brandl5a165582007-08-31 06:15:01 +0000426 exception instances.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000427
Georg Brandlec17d202008-02-02 10:44:37 +0000428* PEP 3109: Raising exceptions. You must now use ``raise Exception(args)``
429 instead of ``raise Exception, args``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000430
Georg Brandle06de8b2008-05-05 21:42:51 +0000431* PEP 3110: Catching exceptions. You must now use ``except SomeException as
432 identifier:`` instead of ``except Exception, identifier:``
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000433
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000434* PEP 3134: Exception chaining.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000435
Georg Brandlec17d202008-02-02 10:44:37 +0000436* A few exception messages are improved when Windows fails to load an extension
437 module. For example, ``error code 193`` is now ``%1 is not a valid Win32
438 application``. Strings now deal with non-English locales.
Georg Brandl396ef802008-02-02 10:30:18 +0000439
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000440
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000441New Class And Metaclass Stuff
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000442=============================
443
444* Classic classes are gone.
445
446* PEP 3115: New Metaclass Syntax.
447
Skip Montanaroa86f5d42007-09-04 02:48:01 +0000448* PEP 3119: Abstract Base Classes (ABCs); ``@abstractmethod`` and
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000449 ``@abstractproperty`` decorators; collection ABCs.
450
451* PEP 3129: Class decorators.
452
453* PEP 3141: Numeric ABCs.
454
455
Georg Brandl116aa622007-08-15 14:28:22 +0000456Other Language Changes
457======================
458
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000459Here are most of the changes that Python 3.0 makes to the core Python
460language and built-in functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000461
Georg Brandlec17d202008-02-02 10:44:37 +0000462* Removed backticks (use :func:`repr` instead).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000463
464* Removed ``<>`` (use ``!=`` instead).
465
Georg Brandl396ef802008-02-02 10:30:18 +0000466* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
467 ``NotImplemented``.
468
Georg Brandlec17d202008-02-02 10:44:37 +0000469* :keyword:`as` and :keyword:`with` are keywords.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000470
Georg Brandl396ef802008-02-02 10:30:18 +0000471* ``True``, ``False``, and ``None`` are keywords.
472
Georg Brandlec17d202008-02-02 10:44:37 +0000473* PEP 237: :class:`long` renamed to :class:`int`. That is, there is only one
474 built-in integral type, named :class:`int`; but it behaves like the old
475 :class:`long` type, with the exception that the literal suffix ``L`` is
476 neither supported by the parser nor produced by :func:`repr` anymore.
477 :data:`sys.maxint` was also removed since the int type has no maximum value
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000478 anymore. Use :data:`sys.maxsize` instead.
479 XXX Is this a dupe from the intro section on integers?
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000480
481* PEP 238: int division returns a float.
482
Georg Brandlec17d202008-02-02 10:44:37 +0000483* The ordering operators behave differently: for example, ``x < y`` where ``x``
484 and ``y`` have incompatible types raises :exc:`TypeError` instead of returning
485 a pseudo-random boolean.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000486
Georg Brandlec17d202008-02-02 10:44:37 +0000487* :meth:`__getslice__` and friends killed. The syntax ``a[i:j]`` now translates
488 to ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
489 :meth:`__delitem__`, depending on context).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000490
Georg Brandlec17d202008-02-02 10:44:37 +0000491* PEP 3102: Keyword-only arguments. Named parameters occurring after ``*args``
492 in the parameter list *must* be specified using keyword syntax in the call.
493 You can also use a bare ``*`` in the parameter list to indicate that you don't
494 accept a variable-length argument list, but you do have keyword-only
495 arguments.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000496
Georg Brandlec17d202008-02-02 10:44:37 +0000497* PEP 3104: :keyword:`nonlocal` statement. Using ``nonlocal x`` you can now
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000498 assign directly to a variable in an outer (but non-global) scope.
499
Georg Brandlec17d202008-02-02 10:44:37 +0000500* PEP 3111: :func:`raw_input` renamed to :func:`input`. That is, the new
501 :func:`input` function reads a line from :data:`sys.stdin` and returns it with
502 the trailing newline stripped. It raises :exc:`EOFError` if the input is
503 terminated prematurely. To get the old behavior of :func:`input`, use
504 ``eval(input())``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000505
Georg Brandl67b8cad2008-04-09 07:32:07 +0000506* :func:`xrange` renamed to :func:`range`, so :func:`range` will no longer
507 produce a list but an iterable yielding integers when iterated over.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000508
Georg Brandlec17d202008-02-02 10:44:37 +0000509* PEP 3113: Tuple parameter unpacking removed. You can no longer write ``def
510 foo(a, (b, c)): ...``. Use ``def foo(a, b_c): b, c = b_c`` instead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000511
Georg Brandlec17d202008-02-02 10:44:37 +0000512* PEP 3114: ``.next()`` renamed to :meth:`__next__`, new builtin :func:`next` to
513 call the :meth:`__next__` method on an object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000514
Georg Brandlec17d202008-02-02 10:44:37 +0000515* PEP 3127: New octal literals; binary literals and :func:`bin`. Instead of
516 ``0666``, you write ``0o666``. The :func:`oct` function is modified
517 accordingly. Also, ``0b1010`` equals 10, and ``bin(10)`` returns
518 ``"0b1010"``. ``0666`` is now a :exc:`SyntaxError`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000519
Georg Brandlec17d202008-02-02 10:44:37 +0000520* PEP 3132: Extended Iterable Unpacking. You can now write things like ``a, b,
521 *rest = some_sequence``. And even ``*rest, a = stuff``. The ``rest`` object
522 is always a list; the right-hand side may be any iterable.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000523
Georg Brandlec17d202008-02-02 10:44:37 +0000524* PEP 3135: New :func:`super`. You can now invoke :func:`super` without
525 arguments and the right class and instance will automatically be chosen. With
526 arguments, its behavior is unchanged.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000527
Georg Brandlec17d202008-02-02 10:44:37 +0000528* :func:`zip`, :func:`map` and :func:`filter` return iterators.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000529
Georg Brandlec17d202008-02-02 10:44:37 +0000530* :data:`string.letters` and its friends (:data:`string.lowercase` and
531 :data:`string.uppercase`) are gone. Use :data:`string.ascii_letters`
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000532 etc. instead.
533
Georg Brandlec17d202008-02-02 10:44:37 +0000534* Removed: :func:`apply`, :func:`callable`, :func:`coerce`, :func:`execfile`,
535 :func:`file`, :func:`reduce`, :func:`reload`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000536
Georg Brandl0327e602008-02-18 22:20:55 +0000537* Removed: :meth:`dict.has_key` -- use the ``in`` operator instead.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000538
Georg Brandlec17d202008-02-02 10:44:37 +0000539* :func:`exec` is now a function.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000540
Georg Brandlec17d202008-02-02 10:44:37 +0000541* The :meth:`__oct__` and :meth:`__hex__` special methods are removed --
542 :func:`oct` and :func:`hex` use :meth:`__index__` now to convert the argument
543 to an integer.
Georg Brandl396ef802008-02-02 10:30:18 +0000544
Georg Brandlec17d202008-02-02 10:44:37 +0000545* Support is removed for :attr:`__members__` and :attr:`__methods__`.
Georg Brandl396ef802008-02-02 10:30:18 +0000546
Georg Brandlec17d202008-02-02 10:44:37 +0000547* Renamed the boolean conversion C-level slot and method: ``nb_nonzero`` is now
548 ``nb_bool`` and :meth:`__nonzero__` is now :meth:`__bool__`.
Georg Brandl396ef802008-02-02 10:30:18 +0000549
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Georg Brandl5a165582007-08-31 06:15:01 +0000551.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553
554Optimizations
555-------------
556
557* Detailed changes are listed here.
558
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000559The net result of the 3.0 generalizations is that Python 3.0 runs the
560pystone benchmark around a third slower than Python 2.5. There's room
561for improvement, but it will happen after 3.0 is released!
Georg Brandl116aa622007-08-15 14:28:22 +0000562
Georg Brandl5a165582007-08-31 06:15:01 +0000563.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000566New, Improved, And Deprecated Modules
Georg Brandl116aa622007-08-15 14:28:22 +0000567=====================================
568
Georg Brandlec17d202008-02-02 10:44:37 +0000569As usual, Python's standard library received a number of enhancements and bug
570fixes. Here's a partial list of the most notable changes, sorted alphabetically
571by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
572complete list of changes, or look through the Subversion logs for all the
573details.
Georg Brandl116aa622007-08-15 14:28:22 +0000574
Georg Brandlec17d202008-02-02 10:44:37 +0000575* The :mod:`cPickle` module is gone. Use :mod:`pickle` instead. Eventually
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000576 we'll have a transparent accelerator module.
Georg Brandl116aa622007-08-15 14:28:22 +0000577
Georg Brandlec17d202008-02-02 10:44:37 +0000578* The :mod:`imageop` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000579
Georg Brandlec17d202008-02-02 10:44:37 +0000580* The :mod:`audiodev`, :mod:`Bastion`, :mod:`bsddb185`, :mod:`exceptions`,
581 :mod:`linuxaudiodev`, :mod:`md5`, :mod:`MimeWriter`, :mod:`mimify`,
582 :mod:`popen2`, :mod:`rexec`, :mod:`sets`, :mod:`sha`, :mod:`stringold`,
583 :mod:`strop`, :mod:`sunaudiodev`, :mod:`timing`, and :mod:`xmllib` modules are
584 gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000585
Gregory P. Smith7b9a2222008-09-04 05:07:03 +0000586* The :mod:`bsddb` module is gone. It is being maintained externally
587 with its own release schedule better mirroring that of BerkeleyDB.
588 See http://www.jcea.es/programacion/pybsddb.htm.
589
Georg Brandlec17d202008-02-02 10:44:37 +0000590* The :mod:`new` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000591
Georg Brandlec17d202008-02-02 10:44:37 +0000592* The functions :func:`os.tmpnam`, :func:`os.tempnam` and :func:`os.tmpfile`
593 have been removed in favor of the :mod:`tempfile` module.
Georg Brandl396ef802008-02-02 10:30:18 +0000594
Trent Nelson428de652008-03-18 22:41:35 +0000595* The :mod:`tokenize` module has been changed to work with bytes. The main
596 entry point is now :func:`tokenize.tokenize`, instead of generate_tokens.
597
Georg Brandl5a165582007-08-31 06:15:01 +0000598.. ======================================================================
599.. whole new modules get described in subsections here
Georg Brandl116aa622007-08-15 14:28:22 +0000600
Georg Brandl5a165582007-08-31 06:15:01 +0000601.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000602
603
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000604Build And C API Changes
Georg Brandl116aa622007-08-15 14:28:22 +0000605=======================
606
607Changes to Python's build process and to the C API include:
608
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000609* PEP 3118: New Buffer API.
610
611* PEP 3121: Extension Module Initialization & Finalization.
612
Georg Brandlec17d202008-02-02 10:44:37 +0000613* PEP 3123: Making :cmacro:`PyObject_HEAD` conform to standard C.
Georg Brandl116aa622007-08-15 14:28:22 +0000614
Georg Brandl396ef802008-02-02 10:30:18 +0000615* No more C API support for restricted execution.
616
Georg Brandlec17d202008-02-02 10:44:37 +0000617* :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`, :cfunc:`PyMember_Get`,
618 and :cfunc:`PyMember_Set` C APIs are removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000619
Georg Brandlec17d202008-02-02 10:44:37 +0000620* New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like
621 :cfunc:`PyImport_ImportModule` but won't block on the import lock (returning
Georg Brandl396ef802008-02-02 10:30:18 +0000622 an error instead).
623
Georg Brandl5a165582007-08-31 06:15:01 +0000624.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000625
626
627Port-Specific Changes
628---------------------
629
630Platform-specific changes go here.
631
Georg Brandl396ef802008-02-02 10:30:18 +0000632
Georg Brandl5a165582007-08-31 06:15:01 +0000633.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000634
635
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000636.. _30section-other:
Georg Brandl116aa622007-08-15 14:28:22 +0000637
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000638Other Changes And Fixes
Georg Brandl116aa622007-08-15 14:28:22 +0000639=======================
640
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000641As usual, there were a bunch of other improvements and bugfixes
642scattered throughout the source tree. A search through the change
643logs finds there were XXX patches applied and YYY bugs fixed between
644Python 2.6 and 3.0. Both figures are likely to be underestimates.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
646Some of the more notable changes are:
647
648* Details go here.
649
Georg Brandl5a165582007-08-31 06:15:01 +0000650.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000653Porting To Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +0000654=====================
655
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000656This section lists previously described changes that may require
657changes to your code:
Georg Brandl116aa622007-08-15 14:28:22 +0000658
659* Everything is all in the details!
660
Georg Brandl72748582008-01-20 10:59:44 +0000661* Developers can include :file:`intobject.h` after :file:`Python.h` for
662 some ``PyInt_`` aliases.
Christian Heimesf78b1c62007-12-02 16:52:32 +0000663
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000664* XXX Mention 2to3.
665
666* XXX Reference external doc about porting extensions?
667
Georg Brandl5a165582007-08-31 06:15:01 +0000668.. ======================================================================