New Objects by Instantiating

Subtyping is only half the story.

Example 2.5. Creating new objects by instantiating

obj = object() 1

cobj = C() 2

mylist = [1,2,3] 3

1 2

The call operator (()) creates a new object by instantiating an existing object. The existing object must be a type object. Depending on the type object, the call operator might accept arguments.

2

Python syntax creates new objects for some built-in types. (The square brackets create an instance of <type 'list'>, for example).

After the above exercise, our slate looks quite full.

Figure 2.3. User Built Objects

User Built Objects

Note that by just subtyping <type 'object'>, the type C automatically is an instance of <type 'type'>. This can be verified by checking C.__class__. Why this happens is explained in the next section.