22 What's New in Python 3.0
33****************************
44
5- :Author: A.M. Kuchling
5+ :Author: A.M. Kuchling, Guido van Rossum
66
77.. |release | replace :: 0.0
88
4848.. % This saves the maintainer the effort of going through the SVN log
4949.. % when researching a change.
5050
51- This article explains the new features in Python 3.0. No release date for
52- Python 3.0 has been set; it will probably be released in mid 2008 .
51+ This 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) .
5353
54- This article doesn't attempt to provide a complete specification of the new
55- features, but instead provides a convenient overview. For full details, you
56- should refer to the documentation for Python 3.0. If you want to understand the
57- complete implementation and design rationale, refer to the PEP for a particular
58- new feature.
54+ The best estimate for a release date is August 2008.
55+
56+ This article doesn't attempt to provide a complete specification of
57+ the new features, but instead provides a convenient overview. For
58+ full details, you should refer to the documentation for Python 3.0. If
59+ you want to understand the complete implementation and design
60+ rationale, refer to the PEP for a particular new feature.
5961
6062.. % Compare with previous release in 2 - 3 sentences here.
6163.. % add hyperlink when the documentation becomes available online.
@@ -68,12 +70,224 @@ new feature.
6870.. % ======================================================================
6971
7072
73+ Common Stumbling Blocks
74+ =======================
75+
76+ This section briefly lists the changes that are more likely to trip
77+ people up, without necessarily raising obvious errors. These are all
78+ explained in more detail below. (I'm not listing syntactic changes
79+ and removed or renamed features here, since those tend to produce hard
80+ and fast errors; it's the subtle behavioral changes in code that
81+ remains syntactically valid that trips people up. I'm also omitting
82+ changes 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+
128+ Strings 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+
158+ PEP 3101: A New Approach to String Formatting
159+ =============================================
160+
161+ XXX
162+
163+
164+ PEP 3106: Revamping ``.keys() ``, ``.items() `` and ``.values() ``
165+ ===============================================================
166+
167+ XXX
168+
169+
170+ PEP 3107: Function Annotations
171+ ==============================
172+
173+ XXX
174+
175+
176+ Exception 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+
196+ New 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+
71211Other Language Changes
72212======================
73213
74- Here are all of the changes that Python 2.6 makes to the core Python language.
214+ Here are most of the changes that Python 3.0 makes to the core Python
215+ language and built-in functions.
216+
217+ * 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.
75290
76- * Detailed changes are listed here.
77291
78292.. % ======================================================================
79293
@@ -83,21 +297,24 @@ Optimizations
83297
84298* Detailed changes are listed here.
85299
86- The net result of the 3.0 optimizations is that Python 3.0 runs the pystone
87- benchmark around XX% slower than Python 2.6.
300+ The net result of the 3.0 generalizations is that Python 3.0 runs the
301+ pystone benchmark around 25% slower than Python 2.5. There's room for
302+ improvement!
88303
89304.. % ======================================================================
90305
91306
92307 New, Improved, and Deprecated Modules
93308=====================================
94309
95- As usual, Python's standard library received a number of enhancements and bug
96- fixes. Here's a partial list of the most notable changes, sorted alphabetically
97- by module name. Consult the :file: `Misc/NEWS ` file in the source tree for a more
98- complete list of changes, or look through the CVS logs for all the details.
310+ As usual, Python's standard library received a number of enhancements
311+ and bug fixes. Here's a partial list of the most notable changes,
312+ sorted alphabetically by module name. Consult the :file: `Misc/NEWS `
313+ file in the source tree for a more complete list of changes, or look
314+ through the CVS logs for all the details.
99315
100- * Detailed changes are listed here.
316+ * The ``cPickle `` module is gone. Use ``pickle `` instead. Eventually
317+ we'll have a transparent accelerator module.
101318
102319.. % ======================================================================
103320.. % whole new modules get described in \subsections here
@@ -110,7 +327,11 @@ Build and C API Changes
110327
111328Changes to Python's build process and to the C API include:
112329
113- * Detailed changes are listed here.
330+ * PEP 3118: New Buffer API.
331+
332+ * PEP 3121: Extension Module Initialization & Finalization.
333+
334+ * PEP 3123: Making PyObject_HEAD conform to standard C.
114335
115336.. % ======================================================================
116337
@@ -128,10 +349,10 @@ Platform-specific changes go here.
128349Other Changes and Fixes
129350=======================
130351
131- As usual, there were a bunch of other improvements and bugfixes scattered
132- throughout the source tree. A search through the change logs finds there were
133- XXX patches applied and YYY bugs fixed between Python 2.6 and 3.0. Both figures
134- are likely to be underestimates.
352+ As usual, there were a bunch of other improvements and bugfixes
353+ scattered throughout the source tree. A search through the change
354+ logs finds there were XXX patches applied and YYY bugs fixed between
355+ Python 2.6 and 3.0. Both figures are likely to be underestimates.
135356
136357Some of the more notable changes are:
137358
@@ -143,8 +364,8 @@ Some of the more notable changes are:
143364 Porting to Python 3.0
144365=====================
145366
146- This section lists previously described changes that may require changes to your
147- code:
367+ This section lists previously described changes that may require
368+ changes to your code:
148369
149370* Everything is all in the details!
150371
@@ -156,6 +377,7 @@ code:
156377Acknowledgements
157378================
158379
159- The author would like to thank the following people for offering suggestions,
160- corrections and assistance with various drafts of this article: .
380+ The author would like to thank the following people for offering
381+ suggestions, corrections and assistance with various drafts of this
382+ article: .
161383
0 commit comments