~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/_sources/whatsnew/3.0.txt

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
****************************
 
2
  What's New In Python 3.0
 
3
****************************
 
4
 
 
5
.. XXX Add trademark info for Apple, Microsoft.
 
6
 
 
7
:Author: Guido van Rossum
 
8
 
 
9
.. $Id$
 
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.  (Note: I didn't get to this for 3.0.
 
19
   GvR.)
 
20
 
 
21
   * This is not a complete list of every single change; completeness
 
22
   is the purpose of Misc/NEWS.  Some changes I consider too small
 
23
   or esoteric to include.  If such a change is added to the text,
 
24
   I'll just remove it.  (This is another reason you shouldn't spend
 
25
   too much time on writing your addition.)
 
26
 
 
27
   * If you want to draw your new text to the attention of the
 
28
   maintainer, add 'XXX' to the beginning of the paragraph or
 
29
   section.
 
30
 
 
31
   * It's OK to just add a fragmentary note about a change.  For
 
32
   example: "XXX Describe the transmogrify() function added to the
 
33
   socket module."  The maintainer will research the change and
 
34
   write the necessary text.
 
35
 
 
36
   * You can comment out your additions if you like, but it's not
 
37
   necessary (especially when a final release is some months away).
 
38
 
 
39
   * Credit the author of a patch or bugfix.   Just the name is
 
40
   sufficient; the e-mail address isn't necessary.  (Due to time
 
41
   constraints I haven't managed to do this for 3.0.  GvR.)
 
42
 
 
43
   * It's helpful to add the bug/patch number as a comment:
 
44
 
 
45
   % Patch 12345
 
46
   XXX Describe the transmogrify() function added to the socket
 
47
   module.
 
48
   (Contributed by P.Y. Developer.)
 
49
 
 
50
   This saves the maintainer the effort of going through the SVN log
 
51
   when researching a change.  (Again, I didn't get to this for 3.0.
 
52
   GvR.)
 
53
 
 
54
This article explains the new features in Python 3.0, compared to 2.6.
 
55
Python 3.0, also known as "Python 3000" or "Py3K", is the first ever
 
56
*intentionally backwards incompatible* Python release.  There are more
 
57
changes than in a typical release, and more that are important for all
 
58
Python users.  Nevertheless, after digesting the changes, you'll find
 
59
that Python really hasn't changed all that much -- by and large, we're
 
60
mostly fixing well-known annoyances and warts, and removing a lot of
 
61
old cruft.
 
62
 
 
63
This article doesn't attempt to provide a complete specification of
 
64
all new features, but instead tries to give a convenient overview.
 
65
For full details, you should refer to the documentation for Python
 
66
3.0, and/or the many PEPs referenced in the text. If you want to
 
67
understand the complete implementation and design rationale for a
 
68
particular feature, PEPs usually have more details than the regular
 
69
documentation; but note that PEPs usually are not kept up-to-date once
 
70
a feature has been fully implemented.
 
71
 
 
72
Due to time constraints this document is not as complete as it should
 
73
have been.  As always for a new release, the ``Misc/NEWS`` file in the
 
74
source distribution contains a wealth of detailed information about
 
75
every small thing that was changed.
 
76
 
 
77
.. Compare with previous release in 2 - 3 sentences here.
 
78
.. add hyperlink when the documentation becomes available online.
 
79
 
 
80
.. ======================================================================
 
81
.. Large, PEP-level features and changes should be described here.
 
82
.. Should there be a new section here for 3k migration?
 
83
.. Or perhaps a more general section describing module changes/deprecation?
 
84
.. sets module deprecated
 
85
.. ======================================================================
 
86
 
 
87
 
 
88
Common Stumbling Blocks
 
89
=======================
 
90
 
 
91
This section lists those few changes that are most likely to trip you
 
92
up if you're used to Python 2.5.
 
93
 
 
94
Print Is A Function
 
95
-------------------
 
96
 
 
97
The ``print`` statement has been replaced with a :func:`print`
 
98
function, with keyword arguments to replace most of the special syntax
 
99
of the old ``print`` statement (:pep:`3105`).  Examples::
 
100
 
 
101
  Old: print "The answer is", 2*2
 
102
  New: print("The answer is", 2*2)
 
103
 
 
104
  Old: print x,           # Trailing comma suppresses newline
 
105
  New: print(x, end=" ")  # Appends a space instead of a newline
 
106
 
 
107
  Old: print              # Prints a newline
 
108
  New: print()            # You must call the function!
 
109
 
 
110
  Old: print >>sys.stderr, "fatal error"
 
111
  New: print("fatal error", file=sys.stderr)
 
112
 
 
113
  Old: print (x, y)       # prints repr((x, y))
 
114
  New: print((x, y))      # Not the same as print(x, y)!
 
115
 
 
116
You can also customize the separator between items, e.g.::
 
117
 
 
118
  print("There are <", 2**32, "> possibilities!", sep="")
 
119
 
 
120
which produces:
 
121
 
 
122
.. code-block:: none
 
123
 
 
124
  There are <4294967296> possibilities!
 
125
 
 
126
Note:
 
127
 
 
128
* The :func:`print` function doesn't support the "softspace" feature of
 
129
  the old ``print`` statement.  For example, in Python 2.x,
 
130
  ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
 
131
  ``print("A\n", "B")`` writes ``"A\n B\n"``.
 
132
 
 
133
* 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!
 
136
 
 
137
* When using the ``2to3`` source-to-source conversion tool, all
 
138
  ``print`` statements are automatically converted to
 
139
  :func:`print` function calls, so this is mostly a non-issue for
 
140
  larger projects.
 
141
 
 
142
Views And Iterators Instead Of Lists
 
143
-------------------------------------
 
144
 
 
145
Some 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 =
 
150
  sorted(d)`` instead (this works in Python 2.5 too and is just
 
151
  as efficient).
 
152
 
 
153
* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
 
154
  :meth:`dict.itervalues` methods are no longer supported.
 
155
 
 
156
* :func:`map` and :func:`filter` return iterators.  If you really need
 
157
  a list and the input sequences are all of equal length, a quick
 
158
  fix is to wrap :func:`map` in :func:`list`, e.g. ``list(map(...))``,
 
159
  but a better fix is
 
160
  often to use a list comprehension (especially when the original code
 
161
  uses :keyword:`lambda`), or rewriting the code so it doesn't need a
 
162
  list at all.  Particularly tricky is :func:`map` invoked for the
 
163
  side effects of the function; the correct transformation is to use a
 
164
  regular :keyword:`for` loop (since creating a list would just be
 
165
  wasteful).
 
166
 
 
167
  If the input sequences are not of equal length, :func:`map` will
 
168
  stop at the termination of the shortest of the sequences. For full
 
169
  compatibility with :func:`map` from Python 2.x, also wrap the sequences in
 
170
  :func:`itertools.zip_longest`, e.g. ``map(func, *sequences)`` becomes
 
171
  ``list(map(func, itertools.zip_longest(*sequences)))``.
 
172
 
 
173
* :func:`range` now behaves like :func:`xrange` used to behave, except
 
174
  it works with values of arbitrary size.  The latter no longer
 
175
  exists.
 
176
 
 
177
* :func:`zip` now returns an iterator.
 
178
 
 
179
Ordering Comparisons
 
180
--------------------
 
181
 
 
182
Python 3.0 has simplified the rules for ordering comparisons:
 
183
 
 
184
* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
 
185
  raise a TypeError exception when the operands don't have a
 
186
  meaningful natural ordering.  Thus, expressions like ``1 < ''``, ``0
 
187
  > None`` or ``len <= len`` are no longer valid, and e.g. ``None <
 
188
  None`` raises :exc:`TypeError` instead of returning
 
189
  ``False``.  A corollary is that sorting a heterogeneous list
 
190
  no longer makes sense -- all the elements must be comparable to each
 
191
  other.  Note that this does not apply to the ``==`` and ``!=``
 
192
  operators: objects of different incomparable types always compare
 
193
  unequal to each other.
 
194
 
 
195
* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the
 
196
  *cmp* argument providing a comparison function.  Use the *key*
 
197
  argument instead. N.B. the *key* and *reverse* arguments are now
 
198
  "keyword-only".
 
199
 
 
200
* The :func:`cmp` function should be treated as gone, and the :meth:`__cmp__`
 
201
  special method is no longer supported.  Use :meth:`__lt__` for sorting,
 
202
  :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as needed.
 
203
  (If you really need the :func:`cmp` functionality, you could use the
 
204
  expression ``(a > b) - (a < b)`` as the equivalent for ``cmp(a, b)``.)
 
205
 
 
206
Integers
 
207
--------
 
208
 
 
209
* :pep:`237`: Essentially, :class:`long` renamed to :class:`int`.
 
210
  That is, there is only one built-in integral type, named
 
211
  :class:`int`; but it behaves mostly like the old :class:`long` type.
 
212
 
 
213
* :pep:`238`: An expression like ``1/2`` returns a float.  Use
 
214
  ``1//2`` to get the truncating behavior.  (The latter syntax has
 
215
  existed for years, at least since Python 2.2.)
 
216
 
 
217
* The :data:`sys.maxint` constant was removed, since there is no
 
218
  longer a limit to the value of integers.  However, :data:`sys.maxsize`
 
219
  can be used as an integer larger than any practical list or string
 
220
  index.  It conforms to the implementation's "natural" integer size
 
221
  and is typically the same as :data:`sys.maxint` in previous releases
 
222
  on the same platform (assuming the same build options).
 
223
 
 
224
* The :func:`repr` of a long integer doesn't include the trailing ``L``
 
225
  anymore, so code that unconditionally strips that character will
 
226
  chop off the last digit instead.  (Use :func:`str` instead.)
 
227
 
 
228
* Octal literals are no longer of the form ``0720``; use ``0o720``
 
229
  instead.
 
230
 
 
231
Text Vs. Data Instead Of Unicode Vs. 8-bit
 
232
------------------------------------------
 
233
 
 
234
Everything you thought you knew about binary data and Unicode has
 
235
changed.
 
236
 
 
237
* Python 3.0 uses the concepts of *text* and (binary) *data* instead
 
238
  of Unicode strings and 8-bit strings.  All text is Unicode; however
 
239
  *encoded* Unicode is represented as binary data.  The type used to
 
240
  hold text is :class:`str`, the type used to hold data is
 
241
  :class:`bytes`.  The biggest difference with the 2.x situation is
 
242
  that any attempt to mix text and data in Python 3.0 raises
 
243
  :exc:`TypeError`, whereas if you were to mix Unicode and 8-bit
 
244
  strings in Python 2.x, it would work if the 8-bit string happened to
 
245
  contain only 7-bit (ASCII) bytes, but you would get
 
246
  :exc:`UnicodeDecodeError` if it contained non-ASCII values.  This
 
247
  value-specific behavior has caused numerous sad faces over the
 
248
  years.
 
249
 
 
250
* As a consequence of this change in philosophy, pretty much all code
 
251
  that uses Unicode, encodings or binary data most likely has to
 
252
  change.  The change is for the better, as in the 2.x world there
 
253
  were numerous bugs having to do with mixing encoded and unencoded
 
254
  text.  To be prepared in Python 2.x, start using :class:`unicode`
 
255
  for all unencoded text, and :class:`str` for binary or encoded data
 
256
  only.  Then the ``2to3`` tool will do most of the work for you.
 
257
 
 
258
* You can no longer use ``u"..."`` literals for Unicode text.
 
259
  However, you must use ``b"..."`` literals for binary data.
 
260
 
 
261
* As the :class:`str` and :class:`bytes` types cannot be mixed, you
 
262
  must always explicitly convert between them.  Use :meth:`str.encode`
 
263
  to go from :class:`str` to :class:`bytes`, and :meth:`bytes.decode`
 
264
  to go from :class:`bytes` to :class:`str`.  You can also use
 
265
  ``bytes(s, encoding=...)`` and ``str(b, encoding=...)``,
 
266
  respectively.
 
267
 
 
268
* Like :class:`str`, the :class:`bytes` type is immutable.  There is a
 
269
  separate *mutable* type to hold buffered binary data,
 
270
  :class:`bytearray`.  Nearly all APIs that accept :class:`bytes` also
 
271
  accept :class:`bytearray`.  The mutable API is based on
 
272
  :class:`collections.MutableSequence`.
 
273
 
 
274
* All backslashes in raw string literals are interpreted literally.
 
275
  This means that ``'\U'`` and ``'\u'`` escapes in raw strings are not
 
276
  treated specially.  For example, ``r'\u20ac'`` is a string of 6
 
277
  characters in Python 3.0, whereas in 2.6, ``ur'\u20ac'`` was the
 
278
  single "euro" character.  (Of course, this change only affects raw
 
279
  string literals; the euro character is ``'\u20ac'`` in Python 3.0.)
 
280
 
 
281
* The built-in :class:`basestring` abstract type was removed.  Use
 
282
  :class:`str` instead.  The :class:`str` and :class:`bytes` types
 
283
  don't have functionality enough in common to warrant a shared base
 
284
  class.  The ``2to3`` tool (see below) replaces every occurrence of
 
285
  :class:`basestring` with :class:`str`.
 
286
 
 
287
* Files opened as text files (still the default mode for :func:`open`)
 
288
  always use an encoding to map between strings (in memory) and bytes
 
289
  (on disk).  Binary files (opened with a ``b`` in the mode argument)
 
290
  always use bytes in memory.  This means that if a file is opened
 
291
  using an incorrect mode or encoding, I/O will likely fail loudly,
 
292
  instead of silently producing incorrect data.  It also means that
 
293
  even Unix users will have to specify the correct mode (text or
 
294
  binary) when opening a file.  There is a platform-dependent default
 
295
  encoding, which on Unixy platforms can be set with the ``LANG``
 
296
  environment variable (and sometimes also with some other
 
297
  platform-specific locale-related environment variables).  In many
 
298
  cases, but not all, the system default is UTF-8; you should never
 
299
  count on this default.  Any application reading or writing more than
 
300
  pure ASCII text should probably have a way to override the encoding.
 
301
  There is no longer any need for using the encoding-aware streams
 
302
  in the :mod:`codecs` module.
 
303
 
 
304
* The initial values of :data:`sys.stdin`, :data:`sys.stdout` and
 
305
  :data:`sys.stderr` are now unicode-only text files (i.e., they are
 
306
  instances of :class:`io.TextIOBase`).  To read and write bytes data
 
307
  with these streams, you need to use their :data:`io.TextIOBase.buffer`
 
308
  attribute.
 
309
 
 
310
* Filenames are passed to and returned from APIs as (Unicode) strings.
 
311
  This can present platform-specific problems because on some
 
312
  platforms filenames are arbitrary byte strings.  (On the other hand,
 
313
  on Windows filenames are natively stored as Unicode.)  As a
 
314
  work-around, most APIs (e.g. :func:`open` and many functions in the
 
315
  :mod:`os` module) that take filenames accept :class:`bytes` objects
 
316
  as well as strings, and a few APIs have a way to ask for a
 
317
  :class:`bytes` return value.  Thus, :func:`os.listdir` returns a
 
318
  list of :class:`bytes` instances if the argument is a :class:`bytes`
 
319
  instance, and :func:`os.getcwdb` returns the current working
 
320
  directory as a :class:`bytes` instance.  Note that when
 
321
  :func:`os.listdir` returns a list of strings, filenames that
 
322
  cannot be decoded properly are omitted rather than raising
 
323
  :exc:`UnicodeError`.
 
324
 
 
325
* Some system APIs like :data:`os.environ` and :data:`sys.argv` can
 
326
  also present problems when the bytes made available by the system is
 
327
  not interpretable using the default encoding.  Setting the ``LANG``
 
328
  variable and rerunning the program is probably the best approach.
 
329
 
 
330
* :pep:`3138`: The :func:`repr` of a string no longer escapes
 
331
  non-ASCII characters.  It still escapes control characters and code
 
332
  points with non-printable status in the Unicode standard, however.
 
333
 
 
334
* :pep:`3120`: The default source encoding is now UTF-8.
 
335
 
 
336
* :pep:`3131`: Non-ASCII letters are now allowed in identifiers.
 
337
  (However, the standard library remains ASCII-only with the exception
 
338
  of contributor names in comments.)
 
339
 
 
340
* The :mod:`StringIO` and :mod:`cStringIO` modules are gone.  Instead,
 
341
  import the :mod:`io` module and use :class:`io.StringIO` or
 
342
  :class:`io.BytesIO` for text and data respectively.
 
343
 
 
344
* See also the :ref:`unicode-howto`, which was updated for Python 3.0.
 
345
 
 
346
 
 
347
Overview Of Syntax Changes
 
348
==========================
 
349
 
 
350
This section gives a brief overview of every *syntactic* change in
 
351
Python 3.0.
 
352
 
 
353
New Syntax
 
354
----------
 
355
 
 
356
* :pep:`3107`: Function argument and return value annotations.  This
 
357
  provides a standardized way of annotating a function's parameters
 
358
  and return value.  There are no semantics attached to such
 
359
  annotations except that they can be introspected at runtime using
 
360
  the :attr:`__annotations__` attribute.  The intent is to encourage
 
361
  experimentation through metaclasses, decorators or frameworks.
 
362
 
 
363
* :pep:`3102`: Keyword-only arguments.  Named parameters occurring
 
364
  after ``*args`` in the parameter list *must* be specified using
 
365
  keyword syntax in the call.  You can also use a bare ``*`` in the
 
366
  parameter list to indicate that you don't accept a variable-length
 
367
  argument list, but you do have keyword-only arguments.
 
368
 
 
369
* Keyword arguments are allowed after the list of base classes in a
 
370
  class definition.  This is used by the new convention for specifying
 
371
  a metaclass (see next section), but can be used for other purposes
 
372
  as well, as long as the metaclass supports it.
 
373
 
 
374
* :pep:`3104`: :keyword:`nonlocal` statement.  Using ``nonlocal x``
 
375
  you can now assign directly to a variable in an outer (but
 
376
  non-global) scope.  :keyword:`nonlocal` is a new reserved word.
 
377
 
 
378
* :pep:`3132`: Extended Iterable Unpacking.  You can now write things
 
379
  like ``a, b, *rest = some_sequence``.  And even ``*rest, a =
 
380
  stuff``.  The ``rest`` object is always a (possibly empty) list; the
 
381
  right-hand side may be any iterable.  Example::
 
382
 
 
383
    (a, *rest, b) = range(5)
 
384
 
 
385
  This sets *a* to ``0``, *b* to ``4``, and *rest* to ``[1, 2, 3]``.
 
386
 
 
387
* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
 
388
  same thing as ``dict(stuff)`` but is more flexible.  (This is
 
389
  :pep:`274` vindicated. :-)
 
390
 
 
391
* Set literals, e.g. ``{1, 2}``.  Note that ``{}`` is an empty
 
392
  dictionary; use ``set()`` for an empty set.  Set comprehensions are
 
393
  also supported; e.g., ``{x for x in stuff}`` means the same thing as
 
394
  ``set(stuff)`` but is more flexible.
 
395
 
 
396
* New octal literals, e.g. ``0o720`` (already in 2.6).  The old octal
 
397
  literals (``0720``) are gone.
 
398
 
 
399
* New binary literals, e.g. ``0b1010`` (already in 2.6), and
 
400
  there is a new corresponding built-in function, :func:`bin`.
 
401
 
 
402
* Bytes literals are introduced with a leading ``b`` or ``B``, and
 
403
  there is a new corresponding built-in function, :func:`bytes`.
 
404
 
 
405
Changed Syntax
 
406
--------------
 
407
 
 
408
* :pep:`3109` and :pep:`3134`: new :keyword:`raise` statement syntax:
 
409
  :samp:`raise [{expr} [from {expr}]]`.  See below.
 
410
 
 
411
* :keyword:`as` and :keyword:`with` are now reserved words.  (Since
 
412
  2.6, actually.)
 
413
 
 
414
* ``True``, ``False``, and ``None`` are reserved words.  (2.6 partially enforced
 
415
  the restrictions on ``None`` already.)
 
416
 
 
417
* Change from :keyword:`except` *exc*, *var* to
 
418
  :keyword:`except` *exc* :keyword:`as` *var*.  See :pep:`3110`.
 
419
 
 
420
* :pep:`3115`: New Metaclass Syntax.  Instead of::
 
421
 
 
422
    class C:
 
423
        __metaclass__ = M
 
424
        ...
 
425
 
 
426
  you must now use::
 
427
 
 
428
    class C(metaclass=M):
 
429
        ...
 
430
 
 
431
  The module-global :data:`__metaclass__` variable is no longer
 
432
  supported.  (It was a crutch to make it easier to default to
 
433
  new-style classes without deriving every class from
 
434
  :class:`object`.)
 
435
 
 
436
* List comprehensions no longer support the syntactic form
 
437
  :samp:`[... for {var} in {item1}, {item2}, ...]`.  Use
 
438
  :samp:`[... for {var} in ({item1}, {item2}, ...)]` instead.
 
439
  Also note that list comprehensions have different semantics: they
 
440
  are closer to syntactic sugar for a generator expression inside a
 
441
  :func:`list` constructor, and in particular the loop control
 
442
  variables are no longer leaked into the surrounding scope.
 
443
 
 
444
* The *ellipsis* (``...``) can be used as an atomic expression
 
445
  anywhere.  (Previously it was only allowed in slices.)  Also, it
 
446
  *must* now be spelled as ``...``.  (Previously it could also be
 
447
  spelled as ``. . .``, by a mere accident of the grammar.)
 
448
 
 
449
Removed Syntax
 
450
--------------
 
451
 
 
452
* :pep:`3113`: Tuple parameter unpacking removed.  You can no longer
 
453
  write ``def foo(a, (b, c)): ...``.
 
454
  Use ``def foo(a, b_c): b, c = b_c`` instead.
 
455
 
 
456
* Removed backticks (use :func:`repr` instead).
 
457
 
 
458
* Removed ``<>`` (use ``!=`` instead).
 
459
 
 
460
* Removed keyword: :func:`exec` is no longer a keyword; it remains as
 
461
  a function.  (Fortunately the function syntax was also accepted in
 
462
  2.x.)  Also note that :func:`exec` no longer takes a stream argument;
 
463
  instead of ``exec(f)`` you can use ``exec(f.read())``.
 
464
 
 
465
* Integer literals no longer support a trailing ``l`` or ``L``.
 
466
 
 
467
* String literals no longer support a leading ``u`` or ``U``.
 
468
 
 
469
* The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only
 
470
  allowed at the module level, no longer inside functions.
 
471
 
 
472
* The only acceptable syntax for relative imports is :samp:`from .[{module}]
 
473
  import {name}`.  All :keyword:`import` forms not starting with ``.`` are
 
474
  interpreted as absolute imports.  (:pep:`328`)
 
475
 
 
476
* Classic classes are gone.
 
477
 
 
478
 
 
479
Changes Already Present In Python 2.6
 
480
=====================================
 
481
 
 
482
Since many users presumably make the jump straight from Python 2.5 to
 
483
Python 3.0, this section reminds the reader of new features that were
 
484
originally designed for Python 3.0 but that were back-ported to Python
 
485
2.6.  The corresponding sections in :ref:`whats-new-in-2.6` should be
 
486
consulted for longer descriptions.
 
487
 
 
488
* :ref:`pep-0343`.  The :keyword:`with` statement is now a standard
 
489
  feature and no longer needs to be imported from the :mod:`__future__`.
 
490
  Also check out :ref:`new-26-context-managers` and
 
491
  :ref:`new-module-contextlib`.
 
492
 
 
493
* :ref:`pep-0366`.  This enhances the usefulness of the :option:`-m`
 
494
  option when the referenced module lives in a package.
 
495
 
 
496
* :ref:`pep-0370`.
 
497
 
 
498
* :ref:`pep-0371`.
 
499
 
 
500
* :ref:`pep-3101`.  Note: the 2.6 description mentions the
 
501
  :meth:`format` method for both 8-bit and Unicode strings.  In 3.0,
 
502
  only the :class:`str` type (text strings with Unicode support)
 
503
  supports this method; the :class:`bytes` type does not.  The plan is
 
504
  to eventually make this the only API for string formatting, and to
 
505
  start deprecating the ``%`` operator in Python 3.1.
 
506
 
 
507
* :ref:`pep-3105`.  This is now a standard feature and no longer needs
 
508
  to be imported from :mod:`__future__`.  More details were given above.
 
509
 
 
510
* :ref:`pep-3110`.  The :keyword:`except` *exc* :keyword:`as` *var*
 
511
  syntax is now standard and :keyword:`except` *exc*, *var* is no
 
512
  longer supported.  (Of course, the :keyword:`as` *var* part is still
 
513
  optional.)
 
514
 
 
515
* :ref:`pep-3112`.  The ``b"..."`` string literal notation (and its
 
516
  variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now
 
517
  produces a literal of type :class:`bytes`.
 
518
 
 
519
* :ref:`pep-3116`.  The :mod:`io` module is now the standard way of
 
520
  doing file I/O.  The built-in :func:`open` function is now an
 
521
  alias for :func:`io.open` and has additional keyword arguments
 
522
  *encoding*, *errors*, *newline* and *closefd*.  Also note that an
 
523
  invalid *mode* argument now raises :exc:`ValueError`, not
 
524
  :exc:`IOError`.  The binary file object underlying a text file
 
525
  object can be accessed as :attr:`f.buffer` (but beware that the
 
526
  text object maintains a buffer of itself in order to speed up
 
527
  the encoding and decoding operations).
 
528
 
 
529
* :ref:`pep-3118`.  The old builtin :func:`buffer` is now really gone;
 
530
  the new builtin :func:`memoryview` provides (mostly) similar
 
531
  functionality.
 
532
 
 
533
* :ref:`pep-3119`.  The :mod:`abc` module and the ABCs defined in the
 
534
  :mod:`collections` module plays a somewhat more prominent role in
 
535
  the language now, and built-in collection types like :class:`dict`
 
536
  and :class:`list` conform to the :class:`collections.MutableMapping`
 
537
  and :class:`collections.MutableSequence` ABCs, respectively.
 
538
 
 
539
* :ref:`pep-3127`.  As mentioned above, the new octal literal
 
540
  notation is the only one supported, and binary literals have been
 
541
  added.
 
542
 
 
543
* :ref:`pep-3129`.
 
544
 
 
545
* :ref:`pep-3141`.  The :mod:`numbers` module is another new use of
 
546
  ABCs, defining Python's "numeric tower".  Also note the new
 
547
  :mod:`fractions` module which implements :class:`numbers.Rational`.
 
548
 
 
549
 
 
550
Library Changes
 
551
===============
 
552
 
 
553
Due to time constraints, this document does not exhaustively cover the
 
554
very extensive changes to the standard library.  :pep:`3108` is the
 
555
reference for the major changes to the library.  Here's a capsule
 
556
review:
 
557
 
 
558
* Many old modules were removed.  Some, like :mod:`gopherlib` (no
 
559
  longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were
 
560
  already deprecated by :pep:`4`.  Others were removed as a result
 
561
  of the removal of support for various platforms such as Irix, BeOS
 
562
  and Mac OS 9 (see :pep:`11`).  Some modules were also selected for
 
563
  removal in Python 3.0 due to lack of use or because a better
 
564
  replacement exists.  See :pep:`3108` for an exhaustive list.
 
565
 
 
566
* The :mod:`bsddb3` package was removed because its presence in the
 
567
  core standard library has proved over time to be a particular burden
 
568
  for the core developers due to testing instability and Berkeley DB's
 
569
  release schedule.  However, the package is alive and well,
 
570
  externally maintained at https://www.jcea.es/programacion/pybsddb.htm.
 
571
 
 
572
* Some modules were renamed because their old name disobeyed
 
573
  :pep:`8`, or for various other reasons.  Here's the list:
 
574
 
 
575
  =======================  =======================
 
576
  Old Name                 New Name
 
577
  =======================  =======================
 
578
  _winreg                  winreg
 
579
  ConfigParser             configparser
 
580
  copy_reg                 copyreg
 
581
  Queue                    queue
 
582
  SocketServer             socketserver
 
583
  markupbase               _markupbase
 
584
  repr                     reprlib
 
585
  test.test_support        test.support
 
586
  =======================  =======================
 
587
 
 
588
* A common pattern in Python 2.x is to have one version of a module
 
589
  implemented in pure Python, with an optional accelerated version
 
590
  implemented as a C extension; for example, :mod:`pickle` and
 
591
  :mod:`cPickle`.  This places the burden of importing the accelerated
 
592
  version and falling back on the pure Python version on each user of
 
593
  these modules.  In Python 3.0, the accelerated versions are
 
594
  considered implementation details of the pure Python versions.
 
595
  Users should always import the standard version, which attempts to
 
596
  import the accelerated version and falls back to the pure Python
 
597
  version.  The :mod:`pickle` / :mod:`cPickle` pair received this
 
598
  treatment.  The :mod:`profile` module is on the list for 3.1.  The
 
599
  :mod:`StringIO` module has been turned into a class in the :mod:`io`
 
600
  module.
 
601
 
 
602
* Some related modules have been grouped into packages, and usually
 
603
  the submodule names have been simplified.  The resulting new
 
604
  packages are:
 
605
 
 
606
  * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`,
 
607
    :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`).
 
608
 
 
609
  * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`).
 
610
 
 
611
  * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`,
 
612
    :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`,
 
613
    :mod:`cookielib`).
 
614
 
 
615
  * :mod:`tkinter` (all :mod:`Tkinter`-related modules except
 
616
    :mod:`turtle`).  The target audience of :mod:`turtle` doesn't
 
617
    really care about :mod:`tkinter`.  Also note that as of Python
 
618
    2.6, the functionality of :mod:`turtle` has been greatly enhanced.
 
619
 
 
620
  * :mod:`urllib` (:mod:`urllib`, :mod:`urllib2`, :mod:`urlparse`,
 
621
    :mod:`robotparse`).
 
622
 
 
623
  * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`,
 
624
    :mod:`SimpleXMLRPCServer`).
 
625
 
 
626
Some other changes to standard library modules, not covered by
 
627
:pep:`3108`:
 
628
 
 
629
* Killed :mod:`sets`.  Use the built-in :func:`set` class.
 
630
 
 
631
* Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`,
 
632
  :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`,
 
633
  :data:`sys.exc_traceback`.  (Note that :data:`sys.last_type`
 
634
  etc. remain.)
 
635
 
 
636
* Cleanup of the :class:`array.array` type: the :meth:`read` and
 
637
  :meth:`write` methods are gone; use :meth:`fromfile` and
 
638
  :meth:`tofile` instead.  Also, the ``'c'`` typecode for array is
 
639
  gone -- use either ``'b'`` for bytes or ``'u'`` for Unicode
 
640
  characters.
 
641
 
 
642
* Cleanup of the :mod:`operator` module: removed
 
643
  :func:`sequenceIncludes` and :func:`isCallable`.
 
644
 
 
645
* Cleanup of the :mod:`thread` module: :func:`acquire_lock` and
 
646
  :func:`release_lock` are gone; use :func:`acquire` and
 
647
  :func:`release` instead.
 
648
 
 
649
* Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API.
 
650
 
 
651
* The :mod:`new` module is gone.
 
652
 
 
653
* The functions :func:`os.tmpnam`, :func:`os.tempnam` and
 
654
  :func:`os.tmpfile` have been removed in favor of the :mod:`tempfile`
 
655
  module.
 
656
 
 
657
* The :mod:`tokenize` module has been changed to work with bytes.  The
 
658
  main entry point is now :func:`tokenize.tokenize`, instead of
 
659
  generate_tokens.
 
660
 
 
661
* :data:`string.letters` and its friends (:data:`string.lowercase` and
 
662
  :data:`string.uppercase`) are gone.  Use
 
663
  :data:`string.ascii_letters` etc. instead.  (The reason for the
 
664
  removal is that :data:`string.letters` and friends had
 
665
  locale-specific behavior, which is a bad idea for such
 
666
  attractively-named global "constants".)
 
667
 
 
668
* Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the
 
669
  underscores, adding an 's').  The :data:`__builtins__` variable
 
670
  found in most global namespaces is unchanged.  To modify a builtin,
 
671
  you should use :mod:`builtins`, not :data:`__builtins__`!
 
672
 
 
673
 
 
674
:pep:`3101`: A New Approach To String Formatting
 
675
================================================
 
676
 
 
677
* A new system for  built-in string formatting operations replaces the
 
678
  ``%`` string  formatting operator.   (However, the ``%``  operator is
 
679
  still supported;  it will  be deprecated in  Python 3.1  and removed
 
680
  from the language at some later time.)  Read :pep:`3101` for the full
 
681
  scoop.
 
682
 
 
683
 
 
684
Changes To Exceptions
 
685
=====================
 
686
 
 
687
The APIs for raising and catching exception have been cleaned up and
 
688
new powerful features added:
 
689
 
 
690
* :pep:`352`: All exceptions must be derived (directly or indirectly)
 
691
  from :exc:`BaseException`.  This is the root of the exception
 
692
  hierarchy.  This is not new as a recommendation, but the
 
693
  *requirement* to inherit from :exc:`BaseException` is new.  (Python
 
694
  2.6 still allowed classic classes to be raised, and placed no
 
695
  restriction on what you can catch.)  As a consequence, string
 
696
  exceptions are finally truly and utterly dead.
 
697
 
 
698
* Almost all exceptions should actually derive from :exc:`Exception`;
 
699
  :exc:`BaseException` should only be used as a base class for
 
700
  exceptions that should only be handled at the top level, such as
 
701
  :exc:`SystemExit` or :exc:`KeyboardInterrupt`.  The recommended
 
702
  idiom for handling all exceptions except for this latter category is
 
703
  to use :keyword:`except` :exc:`Exception`.
 
704
 
 
705
* :exc:`StandardError` was removed.
 
706
 
 
707
* Exceptions no longer behave as sequences.  Use the :attr:`args`
 
708
  attribute instead.
 
709
 
 
710
* :pep:`3109`: Raising exceptions.  You must now use :samp:`raise
 
711
  {Exception}({args})` instead of :samp:`raise {Exception}, {args}`.
 
712
  Additionally, you can no longer explicitly specify a traceback;
 
713
  instead, if you *have* to do this, you can assign directly to the
 
714
  :attr:`__traceback__` attribute (see below).
 
715
 
 
716
* :pep:`3110`: Catching exceptions.  You must now use
 
717
  :samp:`except {SomeException} as {variable}` instead
 
718
  of :samp:`except {SomeException}, {variable}`.  Moreover, the
 
719
  *variable* is explicitly deleted when the :keyword:`except` block
 
720
  is left.
 
721
 
 
722
* :pep:`3134`: Exception chaining.  There are two cases: implicit
 
723
  chaining and explicit chaining.  Implicit chaining happens when an
 
724
  exception is raised in an :keyword:`except` or :keyword:`finally`
 
725
  handler block.  This usually happens due to a bug in the handler
 
726
  block; we call this a *secondary* exception.  In this case, the
 
727
  original exception (that was being handled) is saved as the
 
728
  :attr:`__context__` attribute of the secondary exception.
 
729
  Explicit chaining is invoked with this syntax::
 
730
 
 
731
    raise SecondaryException() from primary_exception
 
732
 
 
733
  (where *primary_exception* is any expression that produces an
 
734
  exception object, probably an exception that was previously caught).
 
735
  In this case, the primary exception is stored on the
 
736
  :attr:`__cause__` attribute of the secondary exception.  The
 
737
  traceback printed when an unhandled exception occurs walks the chain
 
738
  of :attr:`__cause__` and :attr:`__context__` attributes and prints a
 
739
  separate traceback for each component of the chain, with the primary
 
740
  exception at the top.  (Java users may recognize this behavior.)
 
741
 
 
742
* :pep:`3134`: Exception objects now store their traceback as the
 
743
  :attr:`__traceback__` attribute.  This means that an exception
 
744
  object now contains all the information pertaining to an exception,
 
745
  and there are fewer reasons to use :func:`sys.exc_info` (though the
 
746
  latter is not removed).
 
747
 
 
748
* A few exception messages are improved when Windows fails to load an
 
749
  extension module.  For example, ``error code 193`` is now ``%1 is
 
750
  not a valid Win32 application``.  Strings now deal with non-English
 
751
  locales.
 
752
 
 
753
 
 
754
Miscellaneous Other Changes
 
755
===========================
 
756
 
 
757
Operators And Special Methods
 
758
-----------------------------
 
759
 
 
760
* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
 
761
  :data:`NotImplemented`.
 
762
 
 
763
* The concept of "unbound methods" has been removed from the language.
 
764
  When referencing a method as a class attribute, you now get a plain
 
765
  function object.
 
766
 
 
767
* :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__`
 
768
  were killed.  The syntax ``a[i:j]`` now translates to
 
769
  ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
 
770
  :meth:`__delitem__`, when used as an assignment or deletion target,
 
771
  respectively).
 
772
 
 
773
* :pep:`3114`: the standard :meth:`next` method has been renamed to
 
774
  :meth:`~iterator.__next__`.
 
775
 
 
776
* The :meth:`__oct__` and :meth:`__hex__` special methods are removed
 
777
  -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert
 
778
  the argument to an integer.
 
779
 
 
780
* Removed support for :attr:`__members__` and :attr:`__methods__`.
 
781
 
 
782
* The function attributes named :attr:`func_X` have been renamed to
 
783
  use the :data:`__X__` form, freeing up these names in the function
 
784
  attribute namespace for user-defined attributes.  To wit,
 
785
  :attr:`func_closure`, :attr:`func_code`, :attr:`func_defaults`,
 
786
  :attr:`func_dict`, :attr:`func_doc`, :attr:`func_globals`,
 
787
  :attr:`func_name` were renamed to :attr:`__closure__`,
 
788
  :attr:`__code__`, :attr:`__defaults__`, :attr:`~object.__dict__`,
 
789
  :attr:`__doc__`, :attr:`__globals__`, :attr:`~definition.__name__`,
 
790
  respectively.
 
791
 
 
792
* :meth:`__nonzero__` is now :meth:`__bool__`.
 
793
 
 
794
Builtins
 
795
--------
 
796
 
 
797
* :pep:`3135`: New :func:`super`.  You can now invoke :func:`super`
 
798
  without arguments and (assuming this is in a regular instance method
 
799
  defined inside a :keyword:`class` statement) the right class and
 
800
  instance will automatically be chosen.  With arguments, the behavior
 
801
  of :func:`super` is unchanged.
 
802
 
 
803
* :pep:`3111`: :func:`raw_input` was renamed to :func:`input`.  That
 
804
  is, the new :func:`input` function reads a line from
 
805
  :data:`sys.stdin` and returns it with the trailing newline stripped.
 
806
  It raises :exc:`EOFError` if the input is terminated prematurely.
 
807
  To get the old behavior of :func:`input`, use ``eval(input())``.
 
808
 
 
809
* A new built-in function :func:`next` was added to call the
 
810
  :meth:`~iterator.__next__` method on an object.
 
811
 
 
812
* The :func:`round` function rounding strategy and return type have
 
813
  changed.  Exact halfway cases are now rounded to the nearest even
 
814
  result instead of away from zero.  (For example, ``round(2.5)`` now
 
815
  returns ``2`` rather than ``3``.)  ``round(x[, n])`` now
 
816
  delegates to ``x.__round__([n])`` instead of always returning a
 
817
  float.  It generally returns an integer when called with a single
 
818
  argument and a value of the same type as ``x`` when called with two
 
819
  arguments.
 
820
 
 
821
* Moved :func:`intern` to :func:`sys.intern`.
 
822
 
 
823
* Removed: :func:`apply`.  Instead of ``apply(f, args)`` use
 
824
  ``f(*args)``.
 
825
 
 
826
* Removed :func:`callable`.  Instead of ``callable(f)`` you can use
 
827
  ``isinstance(f, collections.Callable)``.  The :func:`operator.isCallable`
 
828
  function is also gone.
 
829
 
 
830
* Removed :func:`coerce`.  This function no longer serves a purpose
 
831
  now that classic classes are gone.
 
832
 
 
833
* Removed :func:`execfile`.  Instead of ``execfile(fn)`` use
 
834
  ``exec(open(fn).read())``.
 
835
 
 
836
* Removed the :class:`file` type.  Use :func:`open`.  There are now several
 
837
  different kinds of streams that open can return in the :mod:`io` module.
 
838
 
 
839
* Removed :func:`reduce`.  Use :func:`functools.reduce` if you really
 
840
  need it; however, 99 percent of the time an explicit :keyword:`for`
 
841
  loop is more readable.
 
842
 
 
843
* Removed :func:`reload`.  Use :func:`imp.reload`.
 
844
 
 
845
* Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator
 
846
  instead.
 
847
 
 
848
.. ======================================================================
 
849
 
 
850
 
 
851
Build and C API Changes
 
852
=======================
 
853
 
 
854
Due to time constraints, here is a *very* incomplete list of changes
 
855
to the C API.
 
856
 
 
857
* Support for several platforms was dropped, including but not limited
 
858
  to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.
 
859
 
 
860
* :pep:`3118`: New Buffer API.
 
861
 
 
862
* :pep:`3121`: Extension Module Initialization & Finalization.
 
863
 
 
864
* :pep:`3123`: Making :c:macro:`PyObject_HEAD` conform to standard C.
 
865
 
 
866
* No more C API support for restricted execution.
 
867
 
 
868
* :c:func:`PyNumber_Coerce`, :c:func:`PyNumber_CoerceEx`,
 
869
  :c:func:`PyMember_Get`, and :c:func:`PyMember_Set` C APIs are removed.
 
870
 
 
871
* New C API :c:func:`PyImport_ImportModuleNoBlock`, works like
 
872
  :c:func:`PyImport_ImportModule` but won't block on the import lock
 
873
  (returning an error instead).
 
874
 
 
875
* Renamed the boolean conversion C-level slot and method:
 
876
  ``nb_nonzero`` is now ``nb_bool``.
 
877
 
 
878
* Removed :c:macro:`METH_OLDARGS` and :c:macro:`WITH_CYCLE_GC` from the C API.
 
879
 
 
880
.. ======================================================================
 
881
 
 
882
 
 
883
Performance
 
884
===========
 
885
 
 
886
The net result of the 3.0 generalizations is that Python 3.0 runs the
 
887
pystone benchmark around 10% slower than Python 2.5.  Most likely the
 
888
biggest cause is the removal of special-casing for small integers.
 
889
There's room for improvement, but it will happen after 3.0 is
 
890
released!
 
891
 
 
892
.. ======================================================================
 
893
 
 
894
 
 
895
Porting To Python 3.0
 
896
=====================
 
897
 
 
898
For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
 
899
best strategy is the following:
 
900
 
 
901
0. (Prerequisite:) Start with excellent test coverage.
 
902
 
 
903
1. Port to Python 2.6.  This should be no more work than the average
 
904
   port from Python 2.x to Python 2.(x+1).  Make sure all your tests
 
905
   pass.
 
906
 
 
907
2. (Still using 2.6:) Turn on the :option:`!-3` command line switch.
 
908
   This enables warnings about features that will be removed (or
 
909
   change) in 3.0.  Run your test suite again, and fix code that you
 
910
   get warnings about until there are no warnings left, and all your
 
911
   tests still pass.
 
912
 
 
913
3. Run the ``2to3`` source-to-source translator over your source code
 
914
   tree.  (See :ref:`2to3-reference` for more on this tool.)  Run the
 
915
   result of the translation under Python 3.0.  Manually fix up any
 
916
   remaining issues, fixing problems until all tests pass again.
 
917
 
 
918
It is not recommended to try to write source code that runs unchanged
 
919
under both Python 2.6 and 3.0; you'd have to use a very contorted
 
920
coding style, e.g. avoiding ``print`` statements, metaclasses,
 
921
and much more.  If you are maintaining a library that needs to support
 
922
both Python 2.6 and Python 3.0, the best approach is to modify step 3
 
923
above by editing the 2.6 version of the source code and running the
 
924
``2to3`` translator again, rather than editing the 3.0 version of the
 
925
source code.
 
926
 
 
927
For porting C extensions to Python 3.0, please see :ref:`cporting-howto`.
 
928
 
 
929
.. ======================================================================