Now that we have our objects and relationships, let's get started. We examine two objects: <type 'object'> and
<type 'type'>.
Example 2.1. Examining <type 'object'> and <type 'type'>
>>> object<type 'object'> >>> type
<type 'type'> >>> object.__class__
<type 'type'> >>> object.__bases__
() >>> type.__class__
<type 'type'> >>> type.__bases__
(<type 'object'>,)
Let's make use of our slate and draw what we've seen.
These two objects are primitive objects in Python. We might as well have introduced them one at a time but that would lead to the chicken and egg problem - which to introduce first? These two objects are interdependent - they cannot stand on their own since they are defined in terms of each other.
Continuing our Python experimentation:
Example 2.2. There's more to <type 'object'> and <type 'type'>
>>> isinstance(object, object)True >>> isinstance(type, object)
True
|
Whoa! What happened here?
This is just Dashed Arrow Up Rule in action. Since |
| Applying both Dashed Arrow Up Rule and Dashed Arrow Down Rule, we can effectively reverse the direction of the dashed arrow. Yes, it is still consistent. |
If the above example proves too confusing, ignore it - it is not much use anyway.
Now for a new concept - type objects. Both the objects we introduced are type objects.
What do we mean by type objects? As we already know, not all objects are equal. In fact, an object can be either a type object or a non-type object. It takes a certain personality to be the 'type of' another object. Objects with this personality are called type objects. Such objects can participate in a type-instance relationship on the type side (as well as instance side). Non-type objects are doomed to always participate only on the instance side.
Also, only type objects can be the 'supertype of' another object. Apparently, you need a strong personality for this as well. In reality, non-type objects are so concrete that it does not make sense for something else to be a subtype. If you still disagree, try to fill in the blank: ________ is a kind of Squasher.
As you guessed, Squasher is a non-type object, and 'snake' is a type object.
Type objects are also lovingly called types. The strong personality of types gets passed down to subtypes, as a result all subtypes of a type object are types themselves.
To summarize:
<type 'object'>is an instance of<type 'type'>.<type 'object'>is a subtype of no object.<type 'type'>is an instance of itself.<type 'type'>is a subtype of<type 'object'>.There are only two kinds of objects in Python: to be unambiguous let's call these types and non-types. Non-types could be called instances, but that term could also refer to a type, since a type is always an instance of another type.
Note that we are drawing arrows on our slate for only the
direct relationships, not the implied ones
(i.e. only if one object is another's __class__, or
in the other's __bases__). This make economic use of
the slate and our mental capacity.
<type 'object'>
>>> type
<type 'type'>
>>> object.__class__
<type 'type'>
>>> object.__bases__
()
>>> type.__class__
<type 'type'>
>>> type.__bases__
(<type 'object'>,)
