blob: ef5b6a363e6b217feb3af198ed39770db950c6ed [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
2 What's New in Python 3.0
3****************************
4
Andrew M. Kuchlingbbb809e2007-09-01 19:26:28 +00005:Author: Guido van Rossum
Georg Brandl5a165582007-08-31 06:15:01 +00006:Release: 0.1
Georg Brandl116aa622007-08-15 14:28:22 +00007
Georg Brandl5a165582007-08-31 06:15:01 +00008.. Rules for maintenance:
9
10 * Anyone can add text to this document. Do not spend very much time
11 on the wording of your changes, because your text will probably
12 get rewritten to some degree.
13
14 * The maintainer will go through Misc/NEWS periodically and add
15 changes; it's therefore more important to add your changes to
16 Misc/NEWS than to this file.
17
18 * This is not a complete list of every single change; completeness
19 is the purpose of Misc/NEWS. Some changes I consider too small
20 or esoteric to include. If such a change is added to the text,
21 I'll just remove it. (This is another reason you shouldn't spend
22 too much time on writing your addition.)
23
24 * If you want to draw your new text to the attention of the
25 maintainer, add 'XXX' to the beginning of the paragraph or
26 section.
27
28 * It's OK to just add a fragmentary note about a change. For
29 example: "XXX Describe the transmogrify() function added to the
30 socket module." The maintainer will research the change and
31 write the necessary text.
32
33 * You can comment out your additions if you like, but it's not
34 necessary (especially when a final release is some months away).
35
36 * Credit the author of a patch or bugfix. Just the name is
37 sufficient; the e-mail address isn't necessary.
38
39 * It's helpful to add the bug/patch number as a comment:
40
41 % Patch 12345
42 XXX Describe the transmogrify() function added to the socket
43 module.
44 (Contributed by P.Y. Developer.)
45
46 This saves the maintainer the effort of going through the SVN log
47 when researching a change.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Guido van Rossumb197f3c2007-08-31 00:37:00 +000049This article explains the new features in Python 3.0, comparing to 2.6
50(or in some cases 2.5, since 2.6 isn't released yet).
Georg Brandl116aa622007-08-15 14:28:22 +000051
Guido van Rossumb197f3c2007-08-31 00:37:00 +000052The best estimate for a release date is August 2008.
53
54This article doesn't attempt to provide a complete specification of
55the new features, but instead provides a convenient overview. For
56full details, you should refer to the documentation for Python 3.0. If
57you want to understand the complete implementation and design
58rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000059
Georg Brandl5a165582007-08-31 06:15:01 +000060.. Compare with previous release in 2 - 3 sentences here.
61.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000062
Georg Brandl5a165582007-08-31 06:15:01 +000063.. ======================================================================
64.. Large, PEP-level features and changes should be described here.
65.. Should there be a new section here for 3k migration?
66.. Or perhaps a more general section describing module changes/deprecation?
67.. sets module deprecated
68.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
Guido van Rossumb197f3c2007-08-31 00:37:00 +000071Common Stumbling Blocks
72=======================
73
74This section briefly lists the changes that are more likely to trip
75people up, without necessarily raising obvious errors. These are all
76explained in more detail below. (I'm not listing syntactic changes
77and removed or renamed features here, since those tend to produce hard
78and fast errors; it's the subtle behavioral changes in code that
79remains syntactically valid that trips people up. I'm also omitting
80changes to rarely used features.)
81
82* Python 3.0 uses strings and bytes instead of the Unicode strings and
83 8-bit strings. This means that pretty much all code that uses
84 Unicode, encodings or binary data in any way has to change. The
85 change is for the better, as in the 2.x world there were numerous
86 bugs having to do with mixing encoded and unencoded text.
87
88* Text files enforce an encoding; binary files use bytes. This means
89 that if a file is opened using an incorrect mode or encoding, I/O
90 will likely fail.
91
92* Bytes aren't hashable, and don't support certain operations like
93 ``b.lower()``, ``b.strip()`` or ``b.split()``.
94 For the latter two, use ``b.strip(b" \t\r\n\f")`` or
95 ``b.split(b" \t\r\n\f")``.
96
97* ``map()`` and ``filter()`` return iterators. A quick fix is e.g.
98 ``list(map(...))``, but a better fix is often to use a list
99 comprehension (especially when the original code uses ``lambda``).
100 Particularly tricky is ``map()`` invoked for the side effects of the
101 function; the correct transformation is to use a for-loop.
102
103* ``dict`` methods ``.keys()``, ``.items()`` and ``.values()`` return
104 views instead of lists. For example, this no longer works:
105 ``k = d.keys(); k.sort()``. Use ``k = sorted(d)`` instead.
106
107* ``1/2`` returns a float. Use ``1//2`` to get the truncating behavior.
108
109* Code that unconditionally strips the trailing ``L`` from the ``repr()``
110 of a long integer will chop off the last digit instead.
111
112* The ``print()`` function doesn't support the "softspace" feature of
113 the old ``print`` statement. For example, in Python 2.x,
114 ``print "A\n", "B\n"`` would write ``"A\nB\n"``; but in Python 3.0,
115 ``print("A\n", "B\n")`` writes ``"A\n B\n"``.
116
117* Also, ``print`` and ``print (x, y)`` behave differently without
118 warning: the former used to add a newline in 2.x, but does nothing
119 in 3.0; the latter used to print the ``repr()`` of a tuple in 2.x,
120 but prints the individual values in 3.0.
121
122* You'll be finding yourself typing ``print x`` a lot in interactive
123 mode. Time to retrain your fingers. :-)
124
125
126Strings and Bytes
127=================
128
Georg Brandl5a165582007-08-31 06:15:01 +0000129* There is only one string type; its name is ``str`` but its behavior
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000130 and implementation are more like ``unicode`` in 2.x.
131
132* PEP 358: There is a new type, ``bytes``, to represent binary data
133 (and encoded text, which is treated as binary data until you decide
134 to decode it). The ``str`` and ``bytes`` types cannot be mixed; you
135 must always explicitly convert between them, using the ``.encode()``
136 (str -> bytes) or ``.decode()`` (bytes -> str) methods. Comparing a
137 bytes and a str instance for equality raises a TypeError; this
138 catches common mistakes.
139
140* PEP 3112: Bytes literals. E.g. b"abc".
141
142* PEP 3120: UTF-8 default source encoding.
143
144* PEP 3131: Non-ASCII identifiers. (However, the standard library
145 remains ASCII-only with the exception of contributor names in
146 comments.)
147
148* PEP 3116: New I/O Implementation. The API is nearly 100% backwards
149 compatible, but completely reimplemented (currently mostly in
150 Python). Also, binary files use bytes instead of strings.
151
152* The ``StringIO`` and ``cStringIO`` modules are gone. Instead,
153 import ``StringIO`` or ``BytesIO`` from the ``io`` module.
154
155
156PEP 3101: A New Approach to String Formatting
157=============================================
158
159XXX
160
161
162PEP 3106: Revamping ``.keys()``, ``.items()`` and ``.values()``
163===============================================================
164
165XXX
166
167
168PEP 3107: Function Annotations
169==============================
170
171XXX
172
173
174Exception Stuff
175===============
176
177* PEP 352: Exceptions must derive from BaseException. This is the
Georg Brandl5a165582007-08-31 06:15:01 +0000178 root of the exception hierarchy.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000179
180* StandardException was removed (already in 2.6).
181
Georg Brandl5a165582007-08-31 06:15:01 +0000182* Dropping sequence behavior (slicing!) and ``.message`` attribute of
183 exception instances.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000184
185* PEP 3109: Raising exceptions. You must now use ``raise
186 Exception(args)`` instead of ``raise Exception, args``.
187
188* PEP 3110: Catching exceptions.
189
190* PEP 3134: Exception chaining. (The ``__context__`` feature from the
191 PEP hasn't been implemented yet in 3.0a1.)
192
193
194New Class and Metaclass Stuff
195=============================
196
197* Classic classes are gone.
198
199* PEP 3115: New Metaclass Syntax.
200
Skip Montanaroa86f5d42007-09-04 02:48:01 +0000201* PEP 3119: Abstract Base Classes (ABCs); ``@abstractmethod`` and
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000202 ``@abstractproperty`` decorators; collection ABCs.
203
204* PEP 3129: Class decorators.
205
206* PEP 3141: Numeric ABCs.
207
208
Georg Brandl116aa622007-08-15 14:28:22 +0000209Other Language Changes
210======================
211
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000212Here are most of the changes that Python 3.0 makes to the core Python
213language and built-in functions.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000215* Removed backticks (use ``repr()`` instead).
216
217* Removed ``<>`` (use ``!=`` instead).
218
219* ``as`` and ``with`` are keywords.
220
221* PEP 237: ``long`` renamed to ``int``. That is, there is only one
222 built-in integral type, named ``int``; but it behaves like the old
223 ``long`` type.
224
225* PEP 238: int division returns a float.
226
227* The ordering operators behave differently: for example, ``x < y``
228 where ``x`` and ``y`` have incompatible types raises ``TypeError``
229 instead of returning a pseudo-random boolean.
230
231* ``__getslice__()`` and friends killed. The syntax ``a[i:j]`` now
232 translates to ``a.__getitem__(slice(i, j))`` (or ``__setitem__``
Georg Brandl5a165582007-08-31 06:15:01 +0000233 or ``__delitem__``, depending on context).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000234
235* PEP 3102: Keyword-only arguments. Named parameters occurring after
236 ``*args`` in the parameter list *must* be specified using keyword
Georg Brandl5a165582007-08-31 06:15:01 +0000237 syntax in the call. You can also use a bare ``*`` in the parameter
238 list to indicate that you don't accept a variable-length argument
239 list, but you do have keyword-only arguments.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000240
241* PEP 3104: ``nonlocal`` statement. Using ``nonlocal x`` you can now
242 assign directly to a variable in an outer (but non-global) scope.
243
Guido van Rossum5eb520c2007-09-04 02:40:38 +0000244* PEP 3105: ``print`` is now a function. Keyword arguments
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000245 ``file=sys.stdout``, ``sep=" "`` and ``end="\n"`` let you customize
246 it.
247
248* PEP 3111: ``raw_input()`` renamed to ``input()``. That is, the new
249 ``input()`` function reads a line from ``sys.stdin`` and returns it
250 with the trailing newline stripped. It raises ``EOFError`` if the
251 input is terminated prematurely. To get the old behavior of
252 ``input()``, use ``eval(input())``.
253
254* ``xrange()`` renamed to ``range()``.
255
256* PEP 3113: Tuple parameter unpacking removed. You can no longer write
257 ``def foo(a, (b, c)): ...``. Use ``def foo(a, b_c): b, c = b_c``
258 instead.
259
Georg Brandl5a165582007-08-31 06:15:01 +0000260* PEP 3114: ``.next()`` renamed to ``.__next__()``, new builtin
261 ``next()`` to call the ``__next__()`` method on an object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000262
263* PEP 3127: New octal literals; binary literals and ``bin()``.
264 Instead of ``0666``, you write ``0o666``. The oct() function is
265 modified accordingly. Also, ``0b1010`` equals 10, and ``bin(10)``
Georg Brandl5a165582007-08-31 06:15:01 +0000266 returns ``"0b1010"``. ``0666`` is now a ``SyntaxError``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000267
268* PEP 3132: Extended Iterable Unpacking. You can now write things
269 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
Georg Brandl5a165582007-08-31 06:15:01 +0000270 stuff``. The ``rest`` object is always a list; the right-hand
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000271 side may be any iterable.
272
273* PEP 3135: New ``super()``. You can now invoke ``super()`` without
274 arguments and the right class and instance will automatically be
275 chosen. With arguments, its behavior is unchanged.
276
277* ``zip()``, ``map()`` and ``filter()`` return iterators.
278
279* ``string.letters`` and its friends (``.lowercase`` and
280 ``.uppercase``) are gone. Use ``string.ascii_letters``
281 etc. instead.
282
Georg Brandl5a165582007-08-31 06:15:01 +0000283* Removed: ``apply()``, ``callable()``, ``coerce()``, ``execfile()``,
284 ``file()``, ``reduce()``, ``reload()``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000285
286* Removed: ``dict.has_key()``.
287
288* ``exec`` is now a function.
289
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Georg Brandl5a165582007-08-31 06:15:01 +0000291.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293
294Optimizations
295-------------
296
297* Detailed changes are listed here.
298
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000299The net result of the 3.0 generalizations is that Python 3.0 runs the
Guido van Rossumb3922cb2007-08-31 14:03:28 +0000300pystone benchmark around 33% slower than Python 2.5. There's room for
301improvement; we expect to be optimizing string and integer operations
302significantly before the final 3.0 release!
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Georg Brandl5a165582007-08-31 06:15:01 +0000304.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000305
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
Georg Brandl5a165582007-08-31 06:15:01 +0000319.. ======================================================================
320.. whole new modules get described in subsections here
Georg Brandl116aa622007-08-15 14:28:22 +0000321
Georg Brandl5a165582007-08-31 06:15:01 +0000322.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000323
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
Georg Brandl5a165582007-08-31 06:15:01 +0000334* PEP 3123: Making ``PyObject_HEAD`` conform to standard C.
Georg Brandl116aa622007-08-15 14:28:22 +0000335
Georg Brandl5a165582007-08-31 06:15:01 +0000336.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338
339Port-Specific Changes
340---------------------
341
342Platform-specific changes go here.
343
Georg Brandl5a165582007-08-31 06:15:01 +0000344.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000345
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
Georg Brandl5a165582007-08-31 06:15:01 +0000361.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000362
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
Georg Brandl5a165582007-08-31 06:15:01 +0000372.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000373
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
Georg Brandl5a165582007-08-31 06:15:01 +0000382article: Georg Brandl.
Georg Brandl116aa622007-08-15 14:28:22 +0000383