blob: e804bb1838b15836da6c66a7283a4873a1c30018 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
2 What's New in Python 3.0
3****************************
4
Guido van Rossumb197f3c2007-08-31 00:37:00 +00005:Author: A.M. Kuchling, Guido van Rossum
Georg Brandl116aa622007-08-15 14:28:22 +00006
7.. |release| replace:: 0.0
8
9.. % $Id: whatsnew26.tex 55506 2007-05-22 07:43:29Z neal.norwitz $
10.. % Rules for maintenance:
11.. %
12.. % * Anyone can add text to this document. Do not spend very much time
13.. % on the wording of your changes, because your text will probably
14.. % get rewritten to some degree.
15.. %
16.. % * The maintainer will go through Misc/NEWS periodically and add
17.. % changes; it's therefore more important to add your changes to
18.. % Misc/NEWS than to this file.
19.. %
20.. % * This is not a complete list of every single change; completeness
21.. % is the purpose of Misc/NEWS. Some changes I consider too small
22.. % or esoteric to include. If such a change is added to the text,
23.. % I'll just remove it. (This is another reason you shouldn't spend
24.. % too much time on writing your addition.)
25.. %
26.. % * If you want to draw your new text to the attention of the
27.. % maintainer, add 'XXX' to the beginning of the paragraph or
28.. % section.
29.. %
30.. % * It's OK to just add a fragmentary note about a change. For
31.. % example: "XXX Describe the transmogrify() function added to the
32.. % socket module." The maintainer will research the change and
33.. % write the necessary text.
34.. %
35.. % * You can comment out your additions if you like, but it's not
36.. % necessary (especially when a final release is some months away).
37.. %
38.. % * Credit the author of a patch or bugfix. Just the name is
39.. % sufficient; the e-mail address isn't necessary.
40.. %
41.. % * It's helpful to add the bug/patch number as a comment:
42.. %
43.. % % Patch 12345
44.. % XXX Describe the transmogrify() function added to the socket
45.. % module.
46.. % (Contributed by P.Y. Developer.)
47.. %
48.. % This saves the maintainer the effort of going through the SVN log
49.. % when researching a change.
50
Guido van Rossumb197f3c2007-08-31 00:37:00 +000051This article explains the new features in Python 3.0, comparing to 2.6
52(or in some cases 2.5, since 2.6 isn't released yet).
Georg Brandl116aa622007-08-15 14:28:22 +000053
Guido van Rossumb197f3c2007-08-31 00:37:00 +000054The best estimate for a release date is August 2008.
55
56This article doesn't attempt to provide a complete specification of
57the new features, but instead provides a convenient overview. For
58full details, you should refer to the documentation for Python 3.0. If
59you want to understand the complete implementation and design
60rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62.. % Compare with previous release in 2 - 3 sentences here.
63.. % add hyperlink when the documentation becomes available online.
64
65.. % ======================================================================
66.. % Large, PEP-level features and changes should be described here.
67.. % Should there be a new section here for 3k migration?
68.. % Or perhaps a more general section describing module changes/deprecation?
69.. % sets module deprecated
70.. % ======================================================================
71
72
Guido van Rossumb197f3c2007-08-31 00:37:00 +000073Common Stumbling Blocks
74=======================
75
76This section briefly lists the changes that are more likely to trip
77people up, without necessarily raising obvious errors. These are all
78explained in more detail below. (I'm not listing syntactic changes
79and removed or renamed features here, since those tend to produce hard
80and fast errors; it's the subtle behavioral changes in code that
81remains syntactically valid that trips people up. I'm also omitting
82changes to rarely used features.)
83
84* Python 3.0 uses strings and bytes instead of the Unicode strings and
85 8-bit strings. This means that pretty much all code that uses
86 Unicode, encodings or binary data in any way has to change. The
87 change is for the better, as in the 2.x world there were numerous
88 bugs having to do with mixing encoded and unencoded text.
89
90* Text files enforce an encoding; binary files use bytes. This means
91 that if a file is opened using an incorrect mode or encoding, I/O
92 will likely fail.
93
94* Bytes aren't hashable, and don't support certain operations like
95 ``b.lower()``, ``b.strip()`` or ``b.split()``.
96 For the latter two, use ``b.strip(b" \t\r\n\f")`` or
97 ``b.split(b" \t\r\n\f")``.
98
99* ``map()`` and ``filter()`` return iterators. A quick fix is e.g.
100 ``list(map(...))``, but a better fix is often to use a list
101 comprehension (especially when the original code uses ``lambda``).
102 Particularly tricky is ``map()`` invoked for the side effects of the
103 function; the correct transformation is to use a for-loop.
104
105* ``dict`` methods ``.keys()``, ``.items()`` and ``.values()`` return
106 views instead of lists. For example, this no longer works:
107 ``k = d.keys(); k.sort()``. Use ``k = sorted(d)`` instead.
108
109* ``1/2`` returns a float. Use ``1//2`` to get the truncating behavior.
110
111* Code that unconditionally strips the trailing ``L`` from the ``repr()``
112 of a long integer will chop off the last digit instead.
113
114* The ``print()`` function doesn't support the "softspace" feature of
115 the old ``print`` statement. For example, in Python 2.x,
116 ``print "A\n", "B\n"`` would write ``"A\nB\n"``; but in Python 3.0,
117 ``print("A\n", "B\n")`` writes ``"A\n B\n"``.
118
119* Also, ``print`` and ``print (x, y)`` behave differently without
120 warning: the former used to add a newline in 2.x, but does nothing
121 in 3.0; the latter used to print the ``repr()`` of a tuple in 2.x,
122 but prints the individual values in 3.0.
123
124* You'll be finding yourself typing ``print x`` a lot in interactive
125 mode. Time to retrain your fingers. :-)
126
127
128Strings and Bytes
129=================
130
131* There is only on string type; its name is ``str`` but its behavior
132 and implementation are more like ``unicode`` in 2.x.
133
134* PEP 358: There is a new type, ``bytes``, to represent binary data
135 (and encoded text, which is treated as binary data until you decide
136 to decode it). The ``str`` and ``bytes`` types cannot be mixed; you
137 must always explicitly convert between them, using the ``.encode()``
138 (str -> bytes) or ``.decode()`` (bytes -> str) methods. Comparing a
139 bytes and a str instance for equality raises a TypeError; this
140 catches common mistakes.
141
142* PEP 3112: Bytes literals. E.g. b"abc".
143
144* PEP 3120: UTF-8 default source encoding.
145
146* PEP 3131: Non-ASCII identifiers. (However, the standard library
147 remains ASCII-only with the exception of contributor names in
148 comments.)
149
150* PEP 3116: New I/O Implementation. The API is nearly 100% backwards
151 compatible, but completely reimplemented (currently mostly in
152 Python). Also, binary files use bytes instead of strings.
153
154* The ``StringIO`` and ``cStringIO`` modules are gone. Instead,
155 import ``StringIO`` or ``BytesIO`` from the ``io`` module.
156
157
158PEP 3101: A New Approach to String Formatting
159=============================================
160
161XXX
162
163
164PEP 3106: Revamping ``.keys()``, ``.items()`` and ``.values()``
165===============================================================
166
167XXX
168
169
170PEP 3107: Function Annotations
171==============================
172
173XXX
174
175
176Exception Stuff
177===============
178
179* PEP 352: Exceptions must derive from BaseException. This is the
180 root of the exception hierarchy; only Exception,
181
182* StandardException was removed (already in 2.6).
183
184* Dropping sequence behavior and ``.message`` attribute of exception
185 instances.
186
187* PEP 3109: Raising exceptions. You must now use ``raise
188 Exception(args)`` instead of ``raise Exception, args``.
189
190* PEP 3110: Catching exceptions.
191
192* PEP 3134: Exception chaining. (The ``__context__`` feature from the
193 PEP hasn't been implemented yet in 3.0a1.)
194
195
196New Class and Metaclass Stuff
197=============================
198
199* Classic classes are gone.
200
201* PEP 3115: New Metaclass Syntax.
202
203* PEP 3119: Abstract Base Classes; ``@abstractmethod`` and
204 ``@abstractproperty`` decorators; collection ABCs.
205
206* PEP 3129: Class decorators.
207
208* PEP 3141: Numeric ABCs.
209
210
Georg Brandl116aa622007-08-15 14:28:22 +0000211Other Language Changes
212======================
213
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000214Here are most of the changes that Python 3.0 makes to the core Python
215language and built-in functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000217* Removed backticks (use ``repr()`` instead).
218
219* Removed ``<>`` (use ``!=`` instead).
220
221* ``as`` and ``with`` are keywords.
222
223* PEP 237: ``long`` renamed to ``int``. That is, there is only one
224 built-in integral type, named ``int``; but it behaves like the old
225 ``long`` type.
226
227* PEP 238: int division returns a float.
228
229* The ordering operators behave differently: for example, ``x < y``
230 where ``x`` and ``y`` have incompatible types raises ``TypeError``
231 instead of returning a pseudo-random boolean.
232
233* ``__getslice__()`` and friends killed. The syntax ``a[i:j]`` now
234 translates to ``a.__getitem__(slice(i, j))`` (or ``__setitem__``
235 or ``__delitem``, depending on context).
236
237* PEP 3102: Keyword-only arguments. Named parameters occurring after
238 ``*args`` in the parameter list *must* be specified using keyword
239 syntax in the call. You can also use ``*`` in the parameter list to
240 indicate that you don't accept a variable-length argument list, but
241 you do have keyword-only arguments.
242
243* PEP 3104: ``nonlocal`` statement. Using ``nonlocal x`` you can now
244 assign directly to a variable in an outer (but non-global) scope.
245
246* PEP 3105: ``print`` is now a function. Keyword argumemts
247 ``file=sys.stdout``, ``sep=" "`` and ``end="\n"`` let you customize
248 it.
249
250* PEP 3111: ``raw_input()`` renamed to ``input()``. That is, the new
251 ``input()`` function reads a line from ``sys.stdin`` and returns it
252 with the trailing newline stripped. It raises ``EOFError`` if the
253 input is terminated prematurely. To get the old behavior of
254 ``input()``, use ``eval(input())``.
255
256* ``xrange()`` renamed to ``range()``.
257
258* PEP 3113: Tuple parameter unpacking removed. You can no longer write
259 ``def foo(a, (b, c)): ...``. Use ``def foo(a, b_c): b, c = b_c``
260 instead.
261
262* PEP 3114: ``.next()`` renamed to ``.__next__()``.
263
264* PEP 3127: New octal literals; binary literals and ``bin()``.
265 Instead of ``0666``, you write ``0o666``. The oct() function is
266 modified accordingly. Also, ``0b1010`` equals 10, and ``bin(10)``
267 returns ``"0b1010"``.
268
269* PEP 3132: Extended Iterable Unpacking. You can now write things
270 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
271 stuff``. The ``rest`` variable is always a list; the right-hand
272 side may be any iterable.
273
274* PEP 3135: New ``super()``. You can now invoke ``super()`` without
275 arguments and the right class and instance will automatically be
276 chosen. With arguments, its behavior is unchanged.
277
278* ``zip()``, ``map()`` and ``filter()`` return iterators.
279
280* ``string.letters`` and its friends (``.lowercase`` and
281 ``.uppercase``) are gone. Use ``string.ascii_letters``
282 etc. instead.
283
284* Removed: apply(), callable(), coerce(), execfile(), file(),
285 reduce(), reload().
286
287* Removed: ``dict.has_key()``.
288
289* ``exec`` is now a function.
290
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292.. % ======================================================================
293
294
295Optimizations
296-------------
297
298* Detailed changes are listed here.
299
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000300The net result of the 3.0 generalizations is that Python 3.0 runs the
301pystone benchmark around 25% slower than Python 2.5. There's room for
302improvement!
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304.. % ======================================================================
305
306
307New, Improved, and Deprecated Modules
308=====================================
309
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000310As usual, Python's standard library received a number of enhancements
311and bug fixes. Here's a partial list of the most notable changes,
312sorted alphabetically by module name. Consult the :file:`Misc/NEWS`
313file in the source tree for a more complete list of changes, or look
314through the CVS logs for all the details.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000316* The ``cPickle`` module is gone. Use ``pickle`` instead. Eventually
317 we'll have a transparent accelerator module.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319.. % ======================================================================
320.. % whole new modules get described in \subsections here
321
322.. % ======================================================================
323
324
325Build and C API Changes
326=======================
327
328Changes to Python's build process and to the C API include:
329
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000330* PEP 3118: New Buffer API.
331
332* PEP 3121: Extension Module Initialization & Finalization.
333
334* PEP 3123: Making PyObject_HEAD conform to standard C.
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336.. % ======================================================================
337
338
339Port-Specific Changes
340---------------------
341
342Platform-specific changes go here.
343
344.. % ======================================================================
345
346
347.. _section-other:
348
349Other Changes and Fixes
350=======================
351
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000352As usual, there were a bunch of other improvements and bugfixes
353scattered throughout the source tree. A search through the change
354logs finds there were XXX patches applied and YYY bugs fixed between
355Python 2.6 and 3.0. Both figures are likely to be underestimates.
Georg Brandl116aa622007-08-15 14:28:22 +0000356
357Some of the more notable changes are:
358
359* Details go here.
360
361.. % ======================================================================
362
363
364Porting to Python 3.0
365=====================
366
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000367This section lists previously described changes that may require
368changes to your code:
Georg Brandl116aa622007-08-15 14:28:22 +0000369
370* Everything is all in the details!
371
372.. % ======================================================================
373
374
375.. _acks:
376
377Acknowledgements
378================
379
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000380The author would like to thank the following people for offering
381suggestions, corrections and assistance with various drafts of this
382article: .
Georg Brandl116aa622007-08-15 14:28:22 +0000383