New Objects by Subtyping

The built-in objects are, well, built into Python. They're there when we start Python, usually there when we finish. So how can we create new objects?

New objects cannot pop out of thin air. They have to be built using existing objects. Specifically, we can only create objects that are at the tail-end of a relationship (i.e. tail-end of an arrow).

Example 2.4. Creating new objects by subtyping

class C(object): 1
    pass 2

class D(object):
    pass

class E(C, D): 3
    pass

class MyList(list): 4
    pass 

1

The class statement tells Python to create a new type object by subtyping an existing type object.

2

The body of a class definition would usually provide useful methods and more.

3

Multiple bases are fine too.

4

Most built-in types can be subtyped (but not all).

After the above example, C.__bases__ contains <type 'object'>, and MyList.__bases__ contains <type 'list'>.

The term class is traditionally used to imply an object created by the class statement. However, classes are now synonymous with types. Built-in types are usually not referred to as classes. This book prefers using the term type for both built-in and user created types.