New Objects by Instantiating

Subclassing 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. Depending on the type, the call operator might accept arguments.


Python syntax creates new objects for some built-in types. The square brackets create an instance of <type 'list'>; a numeric literal creates an instance of <type 'int'>.

After the above exercise, our slate looks quite full.

Figure 2.3. User Built Objects

User Built Objects

Note that by just subclassing <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.