More Built-in Types

Python does not ship with only two objects. Oh no, the two primitives come with a whole gang of buddies.

Figure 2.2. Some Built-in Types

Some Built-in Types

A few built-in types are shown above, and examined below.

Example 2.3. Examining some built-in types

>>> list 1
<type 'list'>
>>> list.__class__  2
<type 'type'>
>>> list.__bases__  3
(<type 'object'>,)
>>> tuple.__class__, tuple.__bases__ 4
(<type 'type'>, (<type 'object'>,))
>>> dict.__class__, dict.__bases__ 5
(<type 'type'>, (<type 'object'>,))
>>>
>>> mylist = [1,2,3] 6
>>> mylist.__class__ 7
<type 'list'>


1

The built-in <type 'list'> object.

2

Its type is <type 'type'>.

3

It has one supertype, <type 'object'>.

4 5

Ditto for <type 'tuple'> and <type 'dict'>.

6

This is how you create an instance of <type 'list'>.

7

How refreshing! For the first time an object that has an object other than <type 'type'> as its type.

When we create a tuple or a dictionary, they are instances of the respective types.

So how can we create an instance of mylist? We cannot. This is because mylist is a non-type object.

We are now in a position to better define type objects.

Type Or Non-type Test Rule

If an object is an instance of <type 'type'>, then it is a type. Otherwise, it is a non-type.

Looking back, we can verify that this is true for all objects we have come across.