More Types to Play With

The following example shows how to discover and experiment with built-in types.

Example 3.1. More built-in types

>>> import types 1
>>> types.ListType is list 2
True
>>> def f(): 3
...     pass
...
>>> f.__class__ is types.FunctionType 4
True
>>>
>>> class MyList(list): 5
...     pass
...
>>> class MyFunction(types.FunctionType): 6
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: type 'function' is not an acceptable base type
>>> dir(types) 7
['BooleanType', 'DictProxyType', 'DictType', ..]



1

The types module contains many built-in types.

2

Some well known types have another name as well.

3

def creates a function object.

4

The type of a function object is types.FunctionType

5

Some built-in types can be subclassed.

6

Some cannot.

7

More types than you can shake a stick at.