Text of docs.python.org › 3.16 › whatsnew › 2.3.html as Styx crawled it on Oct 15, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

What’s New in Python 2.3 — Python 3.16.0a1 documentation

Navigation index modules | next | previous | Python » 3.16.0a1 Documentation » What’s New in Python » What’s New in Python 2.3 | Theme | What’s New in Python 2.3 ¶ Author : A.M. Kuchling 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 sum() and enumerate() . The in operator can now be used for substring searches (e.g. "ab" in "abc" returns 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 sets module contains an implementation of a set datatype.

The Set class is for mutable sets, sets that can have members added and removed. The ImmutableSet class is for sets that can’t be modified, and instances of 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 union() and intersection() methods; an alternative notation uses the bitwise operators & and | .

Mutable sets also have in-place versions of these methods, union_update() and 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 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 issubset() and 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 >>> See also PEP 218 - Adding a Built-In Set Object Type PEP written by Greg V. Wilson. Implemented by Greg V. Wilson, Alex Martelli, and GvR. 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 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 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, yield , was introduced for generators. Any function containing a 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 yield statement, the generator outputs the value of i , similar to a return statement. The big difference between yield and a return statement is that on reaching a 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 yield statement. (For complicated reasons, the yield statement isn’t allowed inside the try block of a try … finally statement; read PEP 255 for a full explanation of the interaction between yield and exceptions.) Here’s a sample usage of the 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 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. 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 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 next() method increment self.count and return it. However, for a moderately complicated generator, writing a corresponding class would be much messier. 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 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 ( https://www2.cs.arizona.edu/icon/ ), where the i

The rest of this page is on the live site.