Classic Classes

This is a note about classic classes in Python. We can create classes of the old (pre 2.2) kind by using a plain class statement.

Example 3.2. Examining classic classes

>>> class ClassicClass: 1
...     pass
...
>>> type(ClassicClass) 2
<type 'classobj'>
>>> import types
>>> types.ClassType is type(ClassicClass) 3
True
>>> types.ClassType.__class__ 4
<type 'type'>
>>> types.ClassType.__bases__ 5
(<type 'object'>,)

1

A class statement specifying no bases creates a classic class Remember that to create a new-style class you must specify object as the base (although this is not required in Python 3.0 since new-style classes are the default). Specifying only classic classes as bases also creates a classic class. Specifying both classic and new-style classes as bases create a new-style class.

2

Its type is an object we haven't seen before (in this book).

3

The type of classic classes is an object called types.ClassType.

4 5

It looks and smells like just another type object.

The types.ClassType object is in some ways an alternative <type 'type'>. Instances of this object (classic classes) are types themselves. The rules of attribute access are different for classic classes and new-style classes. The types.ClassType object exists for backward compatibility and may not exist in future versions of Python. Other sections of this book should not be applied to classic classes.

Comment on this book here: discussion page. I appreciate feedback!

That's all, folks!