Understanding New-Style and Old-Style Classes


Earlier in this chapter, in "Defining a Class," you got a hint that something was afoot in the evolution of classes and objects in the Python language. Beginning in Python 2.2, a new type of class was introduced, called new-style classes. A new-style class is a class that is directly or indirectly based on the new, built-in object introduced in Python 2.2. All of the classes you've seen in this chapter are based on object and are therefore new-style classes. An old-style class is a class that is not based on object, directly or indirectly. If you removed the (object) from any of the Critter class headers in this chapter, you'd have an old-style class. To drive the point home, here's an example of a new-style class header:

 class Critter(object): 

This is the header of an old-style class:

 class Critter: 

Every program you've seen so far in this chapter will work equally well with either new-style or old-style classes. However, new-style classes offer significant improvements over old-style classes. In fact, you'll see one of those improvements at work in the next chapter program, the Property Critter.

HINT

Create new-style classes instead of old-style classes whenever possible. New-style classes can do everything old-style classes can, plus more. Besides, old style classes will cease to exist beginning in Python 3.0.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net