****************************
  What's New in Python 2.3
****************************

:Author: A.M. Kuchling

.. |release| replace:: 1.01

.. $Id: whatsnew23.tex 54631 2007-03-31 11:58:36Z georg.brandl $

This article explains the new features in Python 2.3.  Python 2.3 was released
on July 29, 2003.

The main themes for Python 2.3 are polishing some of the features added in 2.2,
adding various small but useful enhancements to the core language, and expanding
the standard library.  The new object model introduced in the previous version
has benefited from 18 months of bugfixes and from optimization efforts that have
improved the performance of new-style classes.  A few new built-in functions
have been added such as :func:`sum` and :func:`enumerate`.  The :keyword:`in`
operator can now be used for substring searches (e.g. ``"ab" in "abc"`` returns
:const:`True`).

Some of the many new library features include Boolean, set, heap, and date/time
data types, the ability to import modules from ZIP-format archives, metadata
support for the long-awaited Python catalog, an updated version of IDLE, and
modules for logging messages, wrapping text, parsing CSV files, processing
command-line options, using BerkeleyDB databases...  the list of new and
enhanced modules is lengthy.

This article doesn't attempt to provide a complete specification of the new
features, but instead provides a convenient overview.  For full details, you
should refer to the documentation for Python 2.3, such as the Python Library
Reference and the Python Reference Manual.  If you want to understand the
complete implementation and design rationale, refer to the PEP for a particular
new feature.

.. ======================================================================


PEP 218: A Standard Set Datatype
================================

The new :mod:`sets` module contains an implementation of a set datatype.  The
:class:`Set` class is for mutable sets, sets that can have members added and
removed.  The :class:`ImmutableSet` class is for sets that can't be modified,
and instances of :class:`ImmutableSet` can therefore be used as dictionary keys.
Sets are built on top of dictionaries, so the elements within a set must be
hashable.

Here's a simple example::

   >>> import sets
   >>> S = sets.Set([1,2,3])
   >>> S
   Set([1, 2, 3])
   >>> 1 in S
   True
   >>> 0 in S
   False
   >>> S.add(5)
   >>> S.remove(3)
   >>> S
   Set([1, 2, 5])
   >>>

The union and intersection of sets can be computed with the :meth:`union` and
:meth:`intersection` methods; an alternative notation uses the bitwise operators
``&`` and ``|``. Mutable sets also have in-place versions of these methods,
:meth:`union_update` and :meth:`intersection_update`. ::

   >>> S1 = sets.Set([1,2,3])
   >>> S2 = sets.Set([4,5,6])
   >>> S1.union(S2)
   Set([1, 2, 3, 4, 5, 6])
   >>> S1 | S2                  # Alternative notation
   Set([1, 2, 3, 4, 5, 6])
   >>> S1.intersection(S2)
   Set([])
   >>> S1 & S2                  # Alternative notation
   Set([])
   >>> S1.union_update(S2)
   >>> S1
   Set([1, 2, 3, 4, 5, 6])
   >>>

It's also possible to take the symmetric difference of two sets.  This is the
set of all elements in the union that aren't in the intersection.  Another way
of putting it is that the symmetric difference contains all elements that are in
exactly one set.  Again, there's an alternative notation (``^``), and an in-
place version with the ungainly name :meth:`symmetric_difference_update`. ::

   >>> S1 = sets.Set([1,2,3,4])
   >>> S2 = sets.Set([3,4,5,6])
   >>> S1.symmetric_difference(S2)
   Set([1, 2, 5, 6])
   >>> S1 ^ S2
   Set([1, 2, 5, 6])
   >>>

There are also :meth:`issubset` and :meth:`issuperset` methods for checking
whether one set is a subset or superset of another::

   >>> S1 = sets.Set([1,2,3])
   >>> S2 = sets.Set([2,3])
   >>> S2.issubset(S1)
   True
   >>> S1.issubset(S2)
   False
   >>> S1.issuperset(S2)
   True
   >>>


.. seealso::

   :pep:`218` - Adding a Built-In Set Object Type
      PEP written by Greg V. Wilson. Implemented by Greg V. Wilson, Alex Martelli, and
      GvR.

.. ======================================================================


.. _section-generators:

PEP 255: Simple Generators
==========================

In Python 2.2, generators were added as an optional feature, to be enabled by a
``from __future__ import generators`` directive.  In 2.3 generators no longer
need to be specially enabled, and are now always present; this means that
:keyword:`yield` is now always a keyword.  The rest of this section is a copy of
the description of generators from the "What's New in Python 2.2" document; if
you read it back when Python 2.2 came out, you can skip the rest of this
section.

You're doubtless familiar with how function calls work in Python or C. When you
call a function, it gets a private namespace where its local variables are
created.  When the function reaches a :keyword:`return` statement, the local
variables are destroyed and the resulting value is returned to the caller.  A
later call to the same function will get a fresh new set of local variables.
But, what if the local variables weren't thrown away on exiting a function?
What if you could later resume the function where it left off?  This is what
generators provide; they can be thought of as resumable functions.

Here's the simplest example of a generator function::

   def generate_ints(N):
       for i in range(N):
           yield i

A new keyword, :keyword:`yield`, was introduced for generators.  Any function
containing a :keyword:`yield` statement is a generator function; this is
detected by Python's bytecode compiler which compiles the function specially as
a result.

When you call a generator function, it doesn't return a single value; instead it
returns a generator object that supports the iterator protocol.  On executing
the :keyword:`yield` statement, the generator outputs the value of ``i``,
similar to a :keyword:`return` statement.  The big difference between
:keyword:`yield` and a :keyword:`return` statement is that on reaching a
:keyword:`yield` the generator's state of execution is suspended and local
variables are preserved.  On the next call to the generator's ``.next()``
method, the function will resume executing immediately after the
:keyword:`yield` statement.  (For complicated reasons, the :keyword:`yield`
statement isn't allowed inside the :keyword:`try` block of a :keyword:`try`...\
:keyword:`finally` statement; read :pep:`255` for a full explanation of the
interaction between :keyword:`yield` and exceptions.)

Here's a sample usage of the :func:`generate_ints` generator::

   >>> gen = generate_ints(3)
   >>> gen
   <generator object at 0x8117f90>
   >>> gen.next()
   0
   >>> gen.next()
   1
   >>> gen.next()
   2
   >>> gen.next()
   Traceback (most recent call last):
     File "stdin", line 1, in ?
     File "stdin", line 2, in generate_ints
   StopIteration

You could equally write ``for i in generate_ints(5)``, or ``a,b,c =
generate_ints(3)``.

Inside a generator function, the :keyword:`return` statement can only be used
without a value, and signals the end of the procession of values; afterwards the
generator cannot return any further values. :keyword:`return` with a value, such
as ``return 5``, is a syntax error inside a generator function.  The end of the
generator's results can also be indicated by raising :exc:`StopIteration`
manually, or by just letting the flow of execution fall off the bottom of the
function.

You could achieve the effect of generators manually by writing your own class
and storing all the local variables of the generator as instance variables.  For
example, returning a list of integers could be done by setting ``self.count`` to
0, and having the :meth:`next` method increment ``self.count`` and return it.
However, for a moderately complicated generator, writing a corresponding class
would be much messier. :file:`Lib/test/test_generators.py` contains a number of
more interesting examples.  The simplest one implements an in-order traversal of
a tree using generators recursively. ::

   # A recursive generator that generates Tree leaves in in-order.
   def inorder(t):
       if t:
           for x in inorder(t.left):
               yield x
           yield t.label
           for x in inorder(t.right):
               yield x

Two other examples in :file:`Lib/test/test_generators.py` produce solutions for
the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no
queen threatens another) and the Knight's Tour (a route that takes a knight to
every square of an $NxN$ chessboard without visiting any square twice).

The idea of generators comes from other programming languages, especially Icon
(http://www.cs.arizona.edu/icon/), where the idea of generators is central.  In
Icon, every expression and function call behaves like a generator.  One example
from "An Overview of the Icon Programming Language" at
http://www.cs.arizona.edu/icon/docs/ipd266.htm gives an idea of what this looks
like::

   sentence := "Store it in the neighboring harbor"
   if (i := find("or", sentence)) > 5 then write(i)

In Icon the :func:`find` function returns the indexes at which the substring
"or" is found: 3, 23, 33.  In the :keyword:`if` statement, ``i`` is first
assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon
retries it with the second value of 23.  23 is greater than 5, so the comparison
now succeeds, and the code prints the value 23 to the screen.

Python doesn't go nearly as far as Icon in adopting generators as a central
concept.  Generators are considered part of the core Python language, but
learning or using them isn't compulsory; if they don't solve any problems that
you have, feel free to ignore them. One novel feature of Python's interface as
compared to Icon's is that a generator's state is represented as a concrete
object (the iterator) that can be passed around to other functions or stored in
a data structure.


.. seealso::

   :pep:`255` - Simple Generators
      Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland.  Implemented mostly
      by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.

.. ======================================================================


.. _section-encodings:

PEP 263: Source Code Encodings
==============================

Python source files can now be declared as being in different character set
encodings.  Encodings are declared by including a specially formatted comment in
the first or second line of the source file.  For example, a UTF-8 file can be
declared with::

   #!/usr/bin/env python
   # -*- coding: UTF-8 -*-

Without such an encoding declaration, the default encoding used is 7-bit ASCII.
Executing or importing modules that contain string literals with 8-bit
characters and have no encoding declaration will result in a
:exc:`DeprecationWarning` being signalled by Python 2.3; in 2.4 this will be a
syntax error.

The encoding declaration only affects Unicode string literals, which will be
converted to Unicode using the specified encoding.  Note that Python identifiers
are still restricted to ASCII characters, so you can't have variable names that
use characters outside of the usual alphanumerics.


.. seealso::

   :pep:`263` - Defining Python Source Code Encodings
      Written by Marc-André Lemburg and Martin von Löwis; implemented by Suzuki Hisao
      and Martin von Löwis.

.. ======================================================================


PEP 273: Importing Modules from ZIP Archives
============================================

The new :mod:`zipimport` module adds support for importing modules from a ZIP-
format archive.  You don't need to import the module explicitly; it will be
automatically imported if a ZIP archive's filename is added to ``sys.path``.
For example::

   amk@nyman:~/src/python$ unzip -l /tmp/example.zip
   Archive:  /tmp/example.zip
     Length     Date   Time    Name
    --------    ----   ----    ----
        8467  11-26-02 22:30   jwzthreading.py
    --------                   -------
        8467                   1 file
   amk@nyman:~/src/python$ ./python
   Python 2.3 (#1, Aug 1 2003, 19:54:32)
   >>> import sys
   >>> sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
   >>> import jwzthreading
   >>> jwzthreading.__file__
   '/tmp/example.zip/jwzthreading.py'
   >>>

An entry in ``sys.path`` can now be the filename of a ZIP archive. The ZIP
archive can contain any kind of files, but only files named :file:`\*.py`,
:file:`\*.pyc`, or :file:`\*.pyo` can be imported.  If an archive only contains
:file:`\*.py` files, Python will not attempt to modify the archive by adding the
corresponding :file:`\*.pyc` file, meaning that if a ZIP archive doesn't contain
:file:`\*.pyc` files, importing may be rather slow.

A path within the archive can also be specified to only import from a
subdirectory; for example, the path :file:`/tmp/example.zip/lib/` would only
import from the :file:`lib/` subdirectory within the archive.


.. seealso::

   :pep:`273` - Import Modules from Zip Archives
      Written by James C. Ahlstrom,  who also provided an implementation. Python 2.3
      follows the specification in :pep:`273`,  but uses an implementation written by
      Just van Rossum  that uses the import hooks described in :pep:`302`. See section
      :ref:`section-pep302` for a description of the new import hooks.

.. ======================================================================


PEP 277: Unicode file name support for Windows NT
=================================================

On Windows NT, 2000, and XP, the system stores file names as Unicode strings.
Traditionally, Python has represented file names as byte strings, which is
inadequate because it renders some file names inaccessible.

Python now allows using arbitrary Unicode strings (within the limitations of the
file system) for all functions that expect file names, most notably the
:func:`open` built-in function. If a Unicode string is passed to
:func:`os.listdir`, Python now returns a list of Unicode strings.  A new
function, :func:`os.getcwdu`, returns the current directory as a Unicode string.

Byte strings still work as file names, and on Windows Python will transparently
convert them to Unicode using the ``mbcs`` encoding.

Other systems also allow Unicode strings as file names but convert them to byte
strings before passing them to the system, which can cause a :exc:`UnicodeError`
to be raised. Applications can test whether arbitrary Unicode strings are
supported as file names by checking :attr:`os.path.supports_unicode_filenames`,
a Boolean value.

Under MacOS, :func:`os.listdir` may now return Unicode filenames.


.. seealso::

   :pep:`277` - Unicode file name support for Windows NT
      Written by Neil Hodgson; implemented by Neil Hodgson, Martin von Löwis, and Mark
      Hammond.

.. ======================================================================


PEP 278: Universal Newline Support
==================================

The three major operating systems used today are Microsoft Windows, Apple's
Macintosh OS, and the various Unix derivatives.  A minor irritation of cross-
platform work  is that these three platforms all use different characters to
mark the ends of lines in text files.  Unix uses the linefeed (ASCII character
10), MacOS uses the carriage return (ASCII character 13), and Windows uses a
two-character sequence of a carriage return plus a newline.

Python's file objects can now support end of line conventions other than the one
followed by the platform on which Python is running. Opening a file with the
mode ``'U'`` or ``'rU'`` will open a file for reading in universal newline mode.
All three line ending conventions will be translated to a ``'\n'`` in the
strings returned by the various file methods such as :meth:`read` and
:meth:`readline`.

Universal newline support is also used when importing modules and when executing
a file with the :func:`execfile` function.  This means that Python modules can
be shared between all three operating systems without needing to convert the
line-endings.

This feature can be disabled when compiling Python by specifying the
:option:`--without-universal-newlines` switch when running Python's
:program:`configure` script.


.. seealso::

   :pep:`278` - Universal Newline Support
      Written and implemented by Jack Jansen.

.. ======================================================================


.. _section-enumerate:

PEP 279: enumerate()
====================

A new built-in function, :func:`enumerate`, will make certain loops a bit
clearer.  ``enumerate(thing)``, where *thing* is either an iterator or a
sequence, returns a iterator that will return ``(0, thing[0])``, ``(1,
thing[1])``, ``(2, thing[2])``, and so forth.

A common idiom to change every element of a list looks like this::

   for i in range(len(L)):
       item = L[i]
       # ... compute some result based on item ...
       L[i] = result

This can be rewritten using :func:`enumerate` as::

   for i, item in enumerate(L):
       # ... compute some result based on item ...
       L[i] = result


.. seealso::

   :pep:`279` - The enumerate() built-in function
      Written and implemented by Raymond D. Hettinger.

.. ======================================================================


PEP 282: The logging Package
============================

A standard package for writing logs, :mod:`logging`, has been added to Python
2.3.  It provides a powerful and flexible mechanism for generating logging
output which can then be filtered and processed in various ways.  A
configuration file written in a standard format can be used to control the
logging behavior of a program.  Python includes handlers that will write log
records to standard error or to a file or socket, send them to the system log,
or even e-mail them to a particular address; of course, it's also possible to
write your own handler classes.

The :class:`Logger` class is the primary class. Most application code will deal
with one or more :class:`Logger` objects, each one used by a particular
subsystem of the application. Each :class:`Logger` is identified by a name, and
names are organized into a hierarchy using ``.``  as the component separator.
For example, you might have :class:`Logger` instances named ``server``,
``server.auth`` and ``server.network``.  The latter two instances are below
``server`` in the hierarchy.  This means that if you turn up the verbosity for
``server`` or direct ``server`` messages to a different handler, the changes
will also apply to records logged to ``server.auth`` and ``server.network``.
There's also a root :class:`Logger` that's the parent of all other loggers.

For simple uses, the :mod:`logging` package contains some convenience functions
that always use the root log::

   import logging

   logging.debug('Debugging information')
   logging.info('Informational message')
   logging.warning('Warning:config file %s not found', 'server.conf')
   logging.error('Error occurred')
   logging.critical('Critical error -- shutting down')

This produces the following output::

   WARNING:root:Warning:config file server.conf not found
   ERROR:root:Error occurred
   CRITICAL:root:Critical error -- shutting down

In the default configuration, informational and debugging messages are
suppressed and the output is sent to standard error.  You can enable the display
of informational and debugging messages by calling the :meth:`setLevel` method
on the root logger.

Notice the :func:`warning` call's use of string formatting operators; all of the
functions for logging messages take the arguments ``(msg, arg1, arg2, ...)`` and
log the string resulting from ``msg % (arg1, arg2, ...)``.

There's also an :func:`exception` function that records the most recent
traceback.  Any of the other functions will also record the traceback if you
specify a true value for the keyword argument *exc_info*. ::

   def f():
       try:    1/0
       except: logging.exception('Problem recorded')

   f()

This produces the following output::

   ERROR:root:Problem recorded
   Traceback (most recent call last):
     File "t.py", line 6, in f
       1/0
   ZeroDivisionError: integer division or modulo by zero

Slightly more advanced programs will use a logger other than the root logger.
The :func:`getLogger(name)` function is used to get a particular log, creating
it if it doesn't exist yet. :func:`getLogger(None)` returns the root logger. ::

   log = logging.getLogger('server')
    ...
   log.info('Listening on port %i', port)
    ...
   log.critical('Disk full')
    ...

Log records are usually propagated up the hierarchy, so a message logged to
``server.auth`` is also seen by ``server`` and ``root``, but a :class:`Logger`
can prevent this by setting its :attr:`propagate` attribute to :const:`False`.

There are more classes provided by the :mod:`logging` package that can be
customized.  When a :class:`Logger` instance is told to log a message, it
creates a :class:`LogRecord` instance that is sent to any number of different
:class:`Handler` instances.  Loggers and handlers can also have an attached list
of filters, and each filter can cause the :class:`LogRecord` to be ignored or
can modify the record before passing it along.  When they're finally output,
:class:`LogRecord` instances are converted to text by a :class:`Formatter`
class.  All of these classes can be replaced by your own specially-written
classes.

With all of these features the :mod:`logging` package should provide enough
flexibility for even the most complicated applications.  This is only an
incomplete overview of its features, so please see the package's reference
documentation for all of the details.  Reading :pep:`282` will also be helpful.


.. seealso::

   :pep:`282` - A Logging System
      Written by Vinay Sajip and Trent Mick; implemented by Vinay Sajip.

.. ======================================================================


.. _section-bool:

PEP 285: A Boolean Type
=======================

A Boolean type was added to Python 2.3.  Two new constants were added to the
:mod:`__builtin__` module, :const:`True` and :const:`False`.  (:const:`True` and
:const:`False` constants were added to the built-ins in Python 2.2.1, but the
2.2.1 versions are simply set to integer values of 1 and 0 and aren't a
different type.)

The type object for this new type is named :class:`bool`; the constructor for it
takes any Python value and converts it to :const:`True` or :const:`False`. ::

   >>> bool(1)
   True
   >>> bool(0)
   False
   >>> bool([])
   False
   >>> bool( (1,) )
   True

Most of the standard library modules and built-in functions have been changed to
return Booleans. ::

   >>> obj = []
   >>> hasattr(obj, 'append')
   True
   >>> isinstance(obj, list)
   True
   >>> isinstance(obj, tuple)
   False

Python's Booleans were added with the primary goal of making code clearer.  For
example, if you're reading a function and encounter the statement ``return 1``,
you might wonder whether the ``1`` represents a Boolean truth value, an index,
or a coefficient that multiplies some other quantity.  If the statement is
``return True``, however, the meaning of the return value is quite clear.

Python's Booleans were *not* added for the sake of strict type-checking.  A very
strict language such as Pascal would also prevent you performing arithmetic with
Booleans, and would require that the expression in an :keyword:`if` statement
always evaluate to a Boolean result.  Python is not this strict and never will
be, as :pep:`285` explicitly says.  This means you can still use any expression
in an :keyword:`if` statement, even ones that evaluate to a list or tuple or
some random object.  The Boolean type is a subclass of the :class:`int` class so
that arithmetic using a Boolean still works. ::

   >>> True + 1
   2
   >>> False + 1
   1
   >>> False * 75
   0
   >>> True * 75
   75

To sum up :const:`True` and :const:`False` in a sentence: they're alternative
ways to spell the integer values 1 and 0, with the single difference that
:func:`str` and :func:`repr` return the strings ``'True'`` and ``'False'``
instead of ``'1'`` and ``'0'``.


.. seealso::

   :pep:`285` - Adding a bool type
      Written and implemented by GvR.

.. ======================================================================


PEP 293: Codec Error Handling Callbacks
=======================================

When encoding a Unicode string into a byte string, unencodable characters may be
encountered.  So far, Python has allowed specifying the error processing as
either "strict" (raising :exc:`UnicodeError`), "ignore" (skipping the
character), or "replace" (using a question mark in the output string), with
"strict" being the default behavior. It may be desirable to specify alternative
processing of such errors, such as inserting an XML character reference or HTML
entity reference into the converted string.

Python now has a flexible framework to add different processing strategies.  New
error handlers can be added with :func:`codecs.register_error`, and codecs then
can access the error handler with :func:`codecs.lookup_error`. An equivalent C
API has been added for codecs written in C. The error handler gets the necessary
state information such as the string being converted, the position in the string
where the error was detected, and the target encoding.  The handler can then
either raise an exception or return a replacement string.

Two additional error handlers have been implemented using this framework:
"backslashreplace" uses Python backslash quoting to represent unencodable
characters and "xmlcharrefreplace" emits XML character references.


.. seealso::

   :pep:`293` - Codec Error Handling Callbacks
      Written and implemented by Walter Dörwald.

.. ======================================================================


.. _section-pep301:

PEP 301: Package Index and Metadata for Distutils
=================================================

Support for the long-requested Python catalog makes its first appearance in 2.3.

The heart of the catalog is the new Distutils :command:`register` command.
Running ``python setup.py register`` will collect the metadata describing a
package, such as its name, version, maintainer, description, &c., and send it to
a central catalog server.  The resulting catalog is available from
http://www.python.org/pypi.

To make the catalog a bit more useful, a new optional *classifiers* keyword
argument has been added to the Distutils :func:`setup` function.  A list of
`Trove <http://catb.org/~esr/trove/>`_-style strings can be supplied to help
classify the software.

Here's an example :file:`setup.py` with classifiers, written to be compatible
with older versions of the Distutils::

   from distutils import core
   kw = {'name': "Quixote",
         'version': "0.5.1",
         'description': "A highly Pythonic Web application framework",
         # ...
         }

   if (hasattr(core, 'setup_keywords') and
       'classifiers' in core.setup_keywords):
       kw['classifiers'] = \
           ['Topic :: Internet :: WWW/HTTP :: Dynamic Content',
            'Environment :: No Input/Output (Daemon)',
            'Intended Audience :: Developers'],

   core.setup(**kw)

The full list of classifiers can be obtained by running  ``python setup.py
register --list-classifiers``.


.. seealso::

   :pep:`301` - Package Index and Metadata for Distutils
      Written and implemented by Richard Jones.

.. ======================================================================


.. _section-pep302:

PEP 302: New Import Hooks
=========================

While it's been possible to write custom import hooks ever since the
:mod:`ihooks` module was introduced in Python 1.3, no one has ever been really
happy with it because writing new import hooks is difficult and messy.  There
have been various proposed alternatives such as the :mod:`imputil` and :mod:`iu`
modules, but none of them has ever gained much acceptance, and none of them were
easily usable from C code.

:pep:`302` borrows ideas from its predecessors, especially from Gordon
McMillan's :mod:`iu` module.  Three new items  are added to the :mod:`sys`
module:

* ``sys.path_hooks`` is a list of callable objects; most  often they'll be
  classes.  Each callable takes a string containing a path and either returns an
  importer object that will handle imports from this path or raises an
  :exc:`ImportError` exception if it can't handle this path.

* ``sys.path_importer_cache`` caches importer objects for each path, so
  ``sys.path_hooks`` will only need to be traversed once for each path.

* ``sys.meta_path`` is a list of importer objects that will be traversed before
  ``sys.path`` is checked.  This list is initially empty, but user code can add
  objects to it.  Additional built-in and frozen modules can be imported by an
  object added to this list.

Importer objects must have a single method, :meth:`find_module(fullname,
path=None)`.  *fullname* will be a module or package name, e.g. ``string`` or
``distutils.core``.  :meth:`find_module` must return a loader object that has a
single method, :meth:`load_module(fullname)`, that creates and returns the
corresponding module object.

Pseudo-code for Python's new import logic, therefore, looks something like this
(simplified a bit; see :pep:`302` for the full details)::

   for mp in sys.meta_path:
       loader = mp(fullname)
       if loader is not None:
           <module> = loader.load_module(fullname)

   for path in sys.path:
       for hook in sys.path_hooks:
           try:
               importer = hook(path)
           except ImportError:
               # ImportError, so try the other path hooks
               pass
           else:
               loader = importer.find_module(fullname)
               <module> = loader.load_module(fullname)

   # Not found!
   raise ImportError


.. seealso::

   :pep:`302` - New Import Hooks
      Written by Just van Rossum and Paul Moore. Implemented by Just van Rossum.

.. ======================================================================


.. _section-pep305:

PEP 305: Comma-separated Files
==============================

Comma-separated files are a format frequently used for exporting data from
databases and spreadsheets.  Python 2.3 adds a parser for comma-separated files.

Comma-separated format is deceptively simple at first glance::

   Costs,150,200,3.95

Read a line and call ``line.split(',')``: what could be simpler? But toss in
string data that can contain commas, and things get more complicated::

   "Costs",150,200,3.95,"Includes taxes, shipping, and sundry items"

A big ugly regular expression can parse this, but using the new  :mod:`csv`
package is much simpler::

   import csv

   input = open('datafile', 'rb')
   reader = csv.reader(input)
   for line in reader:
       print line

The :func:`reader` function takes a number of different options. The field
separator isn't limited to the comma and can be changed to any character, and so
can the quoting and line-ending characters.

Different dialects of comma-separated files can be defined and registered;
currently there are two dialects, both used by Microsoft Excel. A separate
:class:`csv.writer` class will generate comma-separated files from a succession
of tuples or lists, quoting strings that contain the delimiter.


.. seealso::

   :pep:`305` - CSV File API
      Written and implemented  by Kevin Altis, Dave Cole, Andrew McNamara, Skip
      Montanaro, Cliff Wells.

.. ======================================================================


.. _section-pep307:

PEP 307: Pickle Enhancements
============================

The :mod:`pickle` and :mod:`cPickle` modules received some attention during the
2.3 development cycle.  In 2.2, new-style classes could be pickled without
difficulty, but they weren't pickled very compactly; :pep:`307` quotes a trivial
example where a new-style class results in a pickled string three times longer
than that for a classic class.

The solution was to invent a new pickle protocol.  The :func:`pickle.dumps`
function has supported a text-or-binary flag  for a long time.  In 2.3, this
flag is redefined from a Boolean to an integer: 0 is the old text-mode pickle
format, 1 is the old binary format, and now 2 is a new 2.3-specific format.  A
new constant, :const:`pickle.HIGHEST_PROTOCOL`, can be used to select the
fanciest protocol available.

Unpickling is no longer considered a safe operation.  2.2's :mod:`pickle`
provided hooks for trying to prevent unsafe classes from being unpickled
(specifically, a :attr:`__safe_for_unpickling__` attribute), but none of this
code was ever audited and therefore it's all been ripped out in 2.3.  You should
not unpickle untrusted data in any version of Python.

To reduce the pickling overhead for new-style classes, a new interface for
customizing pickling was added using three special methods:
:meth:`__getstate__`, :meth:`__setstate__`, and :meth:`__getnewargs__`.  Consult
:pep:`307` for the full semantics  of these methods.

As a way to compress pickles yet further, it's now possible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still generate code that doesn't
  execute any assertions.

* Most type objects are now callable, so you can use them to create new objects
  such as functions, classes, and modules.  (This means that the :mod:`new` module
  can be deprecated in a future Python version, because you can now use the type
  objects available in the :mod:`types` module.) For example, you can create a new
  module object with the following code:

  ::

     >>> import types
     >>> m = types.ModuleType('abc','docstring')
     >>> m
     <module 'abc' (built-in)>
     >>> m.__doc__
     'docstring'

* A new warning, :exc:`PendingDeprecationWarning` was added to indicate features
  which are in the process of being deprecated.  The warning will *not* be printed
  by default.  To check for use of features that will be deprecated in the future,
  supply :option:`-Walways::PendingDeprecationWarning::` on the command line or
  use :func:`warnings.filterwarnings`.

* The process of deprecating string-based exceptions, as in ``raise "Error
  occurred"``, has begun.  Raising a string will now trigger
  :exc:`PendingDeprecationWarning`.

* Using ``None`` as a variable name will now result in a :exc:`SyntaxWarning`
  warning.  In a future version of Python, ``None`` may finally become a4, 'greeible to use integer codes
instead of long strings to identify pickled classes. The Python Software
Foundation will maintain a list of standardized codes; there's also a range of
codes for private use.  Currently no codes have been specified.


.. seealso::

   :pep:`307` - Extensions to the pickle protocol
      Written and implemented  by Guido van Rossum and Tim Peters.

.. ======================================================================


.. _section-slices:

Extended Slices
===============

Ever since Python 1.4, the slicing syntax has supported an optional third "step"
or "stride" argument.  For example, these are all legal Python syntax:
``L[1:10:2]``, ``L[:-1:1]``, ``L[::-1]``.  This was added to Python at the
request of the developers of Numerical Python, which uses the third argument
extensively.  However, Python's built-in list, tuple, and string sequence types
have never supported this feature, raising a :exc:`TypeError` if you tried it.
Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even
indexes::

   >>> L = range(10)
   >>> L[::2]
   [0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order::

   >>> L[::-1]
   [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings::

   >>> s='abcd'
   >>> s[::2]
   'ac'
   >>> s[::-1]
   'dcba'

If you have a mutable sequence such as a list or an array you can assign to or
delete an extended slice, but there are some differences between assignment to
extended and regular slices.  Assignment to a regular slice can be used to
change the length of the sequence::

   >>> a = range(3)
   >>> a
   [0, 1, 2]
   >>> a[1:3] = [4, 5, 6]
   >>> a
   [0, 4, 5, 6]

Extended slices aren't this flexible.  When assigning to an extended slice, the
list on the right hand side of the statement must contain the same number of
items as the slice it is replacing::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> a[::2] = [0, -1]
   >>> a
   [0, 1, -1, 3]
   >>> a[::2] = [0,1,2]
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward::

   >>> a = range(4)
   >>> a
   [0, 1, 2, 3]
   >>> a[::2]
   [0, 2]
   >>> del a[::2]
   >>> a
   [1, 3]

One can also now pass slice objects to the :meth:`__getitem__` methods of the
built-in sequences::

   >>> range(10).__getitem__(slice(0, 5, 2))
   [0, 2, 4]

Or use slice objects directly in subscripts::

   >>> range(10)[slice(0, 5, 2)]
   [0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects
now have a method :meth:`indices(length)` which, given the length of a sequence,
returns a ``(start, stop, step)`` tuple that can be passed directly to
:func:`range`. :meth:`indices` handles omitted and out-of-bounds indices in a
manner consistent with regular slices (and this innocuous phrase hides a welter
of confusing details!).  The method is intended to be used like this::

   class FakeSeq:
       ...
       def calc_item(self, i):
           ...
       def __getitem__(self, item):
           if isinstance(item, slice):
               indices = item.indices(len(self))
               return FakeSeq([self.calc_item(i) for i in range(*indices)])
           else:
               return self.calc_item(i)

From this example you can also see that the built-in :class:`slice` object is
now the type object for the slice type, and is no longer a function.  This is
consistent with Python 2.2, where :class:`int`, :class:`str`, etc., underwent
the same change.

.. ======================================================================


Other Language Changes
======================

Here are all of the changes that Python 2.3 makes to the core Python language.

* The :keyword:`yield` statement is now always a keyword, as described in
  section :ref:`section-generators` of this document.

* A new built-in function :func:`enumerate` was added, as described in section
  :ref:`section-enumerate` of this document.

* Two new constants, :const:`True` and :const:`False` were added along with the
  built-in :class:`bool` type, as described in section :ref:`section-bool` of this
  document.

* The :func:`int` type constructor will now return a long integer instead of
  raising an :exc:`OverflowError` when a string or floating-point number is too
  large to fit into an integer.  This can lead to the paradoxical result that
  ``isinstance(int(expression), int)`` is false, but that seems unlikely to cause
  problems in practice.

* Built-in types now support the extended slicing syntax, as described in
  section :ref:`section-slices` of this document.

* A new built-in function, :func:`sum(iterable, start=0)`,  adds up the numeric
  items in the iterable object and returns their sum.  :func:`sum` only accepts
  numbers, meaning that you can't use it to concatenate a bunch of strings.
  (Contributed by Alex Martelli.)

* ``list.insert(pos, value)`` used to  insert *value* at the front of the list
  when *pos* was negative.  The behaviour has now been changed to be consistent
  with slice indexing, so when *pos* is -1 the value will be inserted before the
  last element, and so forth.

* ``list.index(value)``, which searches for *value*  within the list and returns
  its index, now takes optional  *start* and *stop* arguments to limit the search
  to  only part of the list.

* Dictionaries have a new method, :meth:`pop(key[, *default*])`, that returns
  the value corresponding to *key* and removes that key/value pair from the
  dictionary.  If the requested key isn't present in the dictionary, *default* is
  returned if it's specified and :exc:`KeyError` raised if it isn't. ::

     >>> d = {1:2}
     >>> d
     {1: 2}
     >>> d.pop(4)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 4
     >>> d.pop(1)
     2
     >>> d.pop(1)
     Traceback (most recent call last):
       File "stdin", line 1, in ?
     KeyError: 'pop(): dictionary is empty'
     >>> d
     {}
     >>>

  There's also a new class method,  :meth:`dict.fromkeys(iterable, value)`, that
  creates a dictionary with keys taken from the supplied iterator *iterable* and
  all values set to *value*, defaulting to ``None``.

  (Patches contributed by Raymond Hettinger.)

  Also, the :func:`dict` constructor now accepts keyword arguments to simplify
  creating small dictionaries::

     >>> dict(red=1, blue=2, green=3, black=4)
     {'blue': 2, 'black': 4, 'green': 3, 'red': 1}

  (Contributed by Just van Rossum.)

* The :keyword:`assert` statement no longer checks the ``__debug__`` flag, so
  you can no longer disable assertions by assigning to ``__debug__``. Running
  Python with the :option:`-O` switch will still 