New-Style Classes Practical Overview
At our TriZPUG meeting on March 23, 2005, I presented an overview of the main benefits of "new-style" classes that were introduced in Python 2.2.
It is easy to keep on using the "old-style" classes, but it helps for Python programmers to become aware of the new features available to them.
What is so great about new-style classes? From my point of view, the most practical benefits are:
- properties
- super()
- descriptors
- static and class methods
The comments and docstrings in each of these sample modules describe a feature of new-style classes. Put all of the module files in the same directory. Run a module, and it will step through the "interactive session" in the module's docstrings. Press Enter until the the module finishes running. Because these modules use advanced doctest features and also descriptors, Python 2.4 is required.
Module | Description |
---|---|
demotest.py | Extract Python interactive sessions from docstrings and run them in "demo-mode". This module is imported by all the other modules. |
newclass0.py | Intro, syntax of new-style classes |
newclass1.py | properties |
newclass2.py | super() |
newclass3.py | descriptors |
newclass4.py | static and class methods |