Text of docs.python.org › pa › 3 › whatsnew › 2.2.html as Styx crawled it on Nov 19, 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.2 — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » ਪਾਈਥਨ ਵਿੱਚ ਨਵਾਂ ਕੀ ਹੈ » What's New in Python 2.2 | Theme | What's New in Python 2.2 ¶ Author : A.M. Kuchling Introduction ¶ This article explains the new features in Python 2.2.2, released on October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally released on December 21, 2001. Python 2.2 can be thought of as the "cleanup release".

There are some features such as generators and iterators that are completely new, but most of the changes, significant and far-reaching though they may be, are aimed at cleaning up irregularities and dark corners of the language design. 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.2, such as the Python Library Reference and the Python Reference Manual .

If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature. PEPs 252 and 253: Type and Class Changes ¶ The largest and most far-reaching changes in Python 2.2 are to Python's model of objects and classes. The changes should be backward compatible, so it's likely that your code will continue to run unchanged, but the changes provide some amazing new capabilities.

Before beginning this, the longest and most complicated section of this article, I'll provide an overview of the changes and offer some comments. A long time ago I wrote a web page listing flaws in Python's design. One of the most significant flaws was that it's impossible to subclass Python types implemented in C. In particular, it's not possible to subclass built-in types, so you can't just subclass, say, lists in order to add a single useful method to them.

The UserList module provides a class that supports all of the methods of lists and that can be subclassed further, but there's lots of C code that expects a regular Python list and won't accept a UserList instance. Python 2.2 fixes this, and in the process adds some exciting new capabilities. A brief summary: You can subclass built-in types such as lists and even integers, and your subclasses should work in every place that requires the original type.

It's now possible to define static and class methods, in addition to the instance methods available in previous versions of Python. It's also possible to automatically call methods on accessing or setting an instance attribute by using a new mechanism called properties . Many uses of __getattr__() can be rewritten to use properties instead, making the resulting code simpler and faster. As a small side benefit, attributes can now have docstrings, too.

The list of legal attributes for an instance can be limited to a particular set using slots , making it possible to safeguard against typos and perhaps make more optimizations possible in future versions of Python. Some users have voiced concern about all these changes. Sure, they say, the new features are neat and lend themselves to all sorts of tricks that weren't possible in previous versions of Python, but they also make the language more complicated.

Some people have said that they've always recommended Python for its simplicity, and feel that its simplicity is being lost. Personally, I think there's no need to worry. Many of the new features are quite esoteric, and you can write a lot of Python code without ever needed to be aware of them. Writing a simple class is no more difficult than it ever was, so you don't need to bother learning or teaching them unless they're actually needed.

Some very complicated tasks that were previously only possible from C will now be possible in pure Python, and to my mind that's all for the better. I'm not going to attempt to cover every single corner case and small change that were required to make the new features work. Instead this section will paint only the broad strokes. See section Related Links , "Related Links", for further sources of information about Python 2.2's new object model.

Old and New Classes ¶ First, you should know that Python 2.2 really has two kinds of classes: classic or old-style classes, and new-style classes. The old-style class model is exactly the same as the class model in earlier versions of Python. All the new features described in this section apply only to new-style classes. This divergence isn't intended to last forever; eventually old-style classes will be dropped, possibly in Python 3.0.

So how do you define a new-style class? You do it by subclassing an existing new-style class. Most of Python's built-in types, such as integers, lists, dictionaries, and even files, are new-style classes now. A new-style class named object , the base class for all built-in types, has also been added so if no built-in type is suitable, you can just subclass object : class C ( object ): def __init__ ( self ): ... ... This means that class statements that don't have any base classes are always classic classes in Python 2.2. (Actually you can also change this by setting a module-level variable named __metaclass__ --- see PEP 253 for the details --- but it's easier to just subclass object .) The type objects for the built-in types are available as built-ins, named using a clever trick.

Python has always had built-in functions named int() , float() , and str() . In 2.2, they aren't functions any more, but type objects that behave as factories when called. >>> int <type 'int'> >>> int ( '123' ) 123 To make the set of types complete, new type objects such as dict() and file() have been added. Here's a more interesting example, adding a lock() method to file objects: class LockableFile ( file ): def lock ( self , operation , length = 0 , start = 0 , whence = 0 ): import fcntl return fcntl . lockf ( self . fileno (), operation , length , start , whence ) The now-obsolete posixfile module contained a class that emulated all of a file object's methods and also added a lock() method, but this class couldn't be passed to internal functions that expected a built-in file, something which is possible with our new LockableFile .

Descriptors ¶ In previous versions of Python, there was no consistent way to discover what attributes and methods were supported by an object. There were some informal conventions, such as defining __members__ and __methods__ attributes that were lists of names, but often the author of an extension type or a class wouldn't bother to define them. You could fall back on inspecting the __dict__ of an object, but when class inheritance or an arbitrary __getattr__() hook were in use this could still be inaccurate.

The one big idea underlying the new class model is that an API for describing the attributes of an object using descriptors has been formalized. Descriptors specify the value of an attribute, stating whether it's a method or a field. With the descriptor API, static methods and class methods become possible, as well as more exotic constructs. Attribute descriptors are objects that live inside class objects, and have a few attributes of their own: __name__ is the attribute's name. __doc__ is the attribute's docstring. __get__(object) is a method that retrieves the attribute value from object . __set__(object, value) sets the attribute on object to value . __delete__(object, value) deletes the value attribute of object .

For example, when you write obj.x , the steps that Python actually performs are: descriptor = obj . __class__ . x descriptor . __get__ ( obj ) For methods, descriptor.__get__ returns a temporary object that's callable, and wraps up the instance and the method to be called on it. This is also why static methods and class methods are now possible; they have descriptors that wrap up just the method, or the method and the class.

As a brief explanation of these new kinds of methods, static methods aren't passed the instance

The rest of this page is on the live site.