Text of docs.python.org › 3.15 › whatsnew › 2.4.html as Styx crawled it on Nov 20, 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.4 — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » What’s New in Python » What’s New in Python 2.4 | Theme | What’s New in Python 2.4 ¶ Author : A.M. Kuchling This article explains the new features in Python 2.4.1, released on March 30, 2005. Python 2.4 is a medium-sized release. It doesn’t introduce as many changes as the radical Python 2.2, but introduces more features than the conservative 2.3 release.

The most significant new language features are function decorators and generator expressions; most other changes are to the standard library. According to the CVS change logs, there were 481 patches applied and 502 bugs fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates. This article doesn’t attempt to provide a complete specification of every single new feature, but instead provides a brief introduction to each feature.

For full details, you should refer to the documentation for Python 2.4, such as the Python Library Reference and the Python Reference Manual. Often you will be referred to the PEP for a particular new feature for explanations of the implementation and design rationale. PEP 218: Built-In Set Objects ¶ Python 2.3 introduced the sets module. C implementations of set data types have now been added to the Python core as two new built-in types, set(iterable) and frozenset(iterable) .

They provide high speed operations for membership testing, for eliminating duplicates from sequences, and for mathematical operations like unions, intersections, differences, and symmetric differences. >>> a = set ( 'abracadabra' ) # form a set from a string >>> 'z' in a # fast membership testing False >>> a # unique letters in a set(['a', 'r', 'b', 'c', 'd']) >>> '' . join ( a ) # convert back into a string 'arbcd' >>> b = set ( 'alacazam' ) # form a second set >>> a - b # letters in a but not in b set(['r', 'd', 'b']) >>> a | b # letters in either a or b set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) >>> a & b # letters in both a and b set(['a', 'c']) >>> a ^ b # letters in a or b but not both set(['r', 'd', 'b', 'm', 'z', 'l']) >>> a . add ( 'z' ) # add a new element >>> a . update ( 'wxy' ) # add multiple new elements >>> a set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) >>> a . remove ( 'x' ) # take one element out >>> a set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) The frozenset() type is an immutable version of set() .

Since it is immutable and hashable, it may be used as a dictionary key or as a member of another set. The sets module remains in the standard library, and may be useful if you wish to subclass the Set or ImmutableSet classes. There are currently no plans to deprecate the module. See also PEP 218 - Adding a Built-In Set Object Type Originally proposed by Greg Wilson and ultimately implemented by Raymond Hettinger. PEP 237: Unifying Long Integers and Integers ¶ The lengthy transition process for this PEP, begun in Python 2.2, takes another step forward in Python 2.4.

In 2.3, certain integer operations that would behave differently after int/long unification triggered FutureWarning warnings and returned values limited to 32 or 64 bits (depending on your platform). In 2.4, these expressions no longer produce a warning and instead produce a different result that’s usually a long integer. The problematic expressions are primarily left shifts and lengthy hexadecimal and octal constants.

For example, 2 << 32 results in a warning in 2.3, evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns the correct answer, 8589934592. See also PEP 237 - Unifying Long Integers and Integers Original PEP written by Moshe Zadka and GvR. The changes for 2.4 were implemented by Kalle Svensson. PEP 289: Generator Expressions ¶ The iterator feature introduced in Python 2.2 and the itertools module make it easier to write programs that loop through large data sets without having the entire data set in memory at one time.

List comprehensions don’t fit into this picture very well because they produce a Python list object containing all of the items. This unavoidably pulls all of the objects into memory, which can be a problem if your data set is very large. When trying to write a functionally styled program, it would be natural to write something like: links = [ link for link in get_all_links () if not link . followed ] for link in links : ... instead of for link in get_all_links (): if link . followed : continue ...

The first form is more concise and perhaps more readable, but if you’re dealing with a large number of link objects you’d have to write the second form to avoid having all link objects in memory at the same time. Generator expressions work similarly to list comprehensions but don’t materialize the entire list; instead they create a generator that will return elements one by one. The above example could be written as: links = ( link for link in get_all_links () if not link . followed ) for link in links : ...

Generator expressions always have to be written inside parentheses, as in the above example. The parentheses signalling a function call also count, so if you want to create an iterator that will be immediately passed to a function you could write: print sum ( obj . count for obj in list_all_objects ()) Generator expressions differ from list comprehensions in various small ways. Most notably, the loop variable ( obj in the above example) is not accessible outside of the generator expression.

List comprehensions leave the variable assigned to its last value; future versions of Python will change this, making list comprehensions match generator expressions in this respect. See also PEP 289 - Generator Expressions Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang. PEP 292: Simpler String Substitutions ¶ Some new classes in the standard library provide an alternative mechanism for substituting variables into strings; this style of substitution may be better for applications where untrained users need to edit templates.

The usual way of substituting variables by name is the % operator: >>> ' %(page)i : %(title)s ' % { 'page' : 2 , 'title' : 'The Best of Times' } '2: The Best of Times' When writing the template string, it can be easy to forget the i or s after the closing parenthesis. This isn’t a big problem if the template is in a Python module, because you run the code, get an “Unsupported format character” ValueError , and fix the problem.

However, consider an application such as Mailman where template strings or translations are being edited by users who aren’t aware of the Python language. The format string’s syntax is complicated to explain to such users, and if they make a mistake, it’s difficult to provide helpful feedback to them. PEP 292 adds a Template class to the string module that uses $ to indicate a substitution: >>> import string >>> t = string .

Template ( '$page: $title' ) >>> t . substitute ({ 'page' : 2 , 'title' : 'The Best of Times' }) '2: The Best of Times' If a key is missing from the dictionary, the substitute() method will raise a KeyError . There’s also a safe_substitute() method that ignores missing keys: >>> t = string . Template ( '$page: $title' ) >>> t . safe_substitute ({ 'page' : 3 }) '3: $title' See also PEP 292 - Simpler String Substitutions Written and implemented by Barry Warsaw.

PEP 318: Decorators for Functions and Methods ¶ Python 2.2 extended Python’s object model by adding static methods and class methods, but it didn’t extend Python’s syntax to provide any new way of defining static or class methods. Instead, you had to write a def statement in the usual way, and pass the resulting method to a staticmethod() or classmethod() function that would wrap up the function as a method of the new type.

Your code would look like this: class C : def meth ( cls ): ... meth = classmethod ( meth ) # Rebind name to wrapped-up class method If the

The rest of this page is on the live site.