预计阅读本页时间:-
Classes Are Instances of type
So far in this book, we’ve done most of our work by making instances of built-in types like lists and strings, as well as instances of classes we code ourselves. As we’ve seen, instances of classes have some state information attributes of their own, but they also inherit behavioral attributes from the classes from which they are made. The same holds true for built-in types; list instances, for example, have values of their own, but they inherit methods from the list type.
While we can get a lot done with such instance objects, Python’s type model turns out to be a bit richer than I’ve formally described. Really, there’s a hole in the model we’ve seen thus far: if instances are created from classes, what is it that creates our classes? It turns out that classes are instances of something, too:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
- In Python 3.0, user-defined class objects are instances of the object named type, which is itself a class.
- In Python 2.6, new-style classes inherit from object, which is a subclass of type; classic classes are instances of type and are not created from a class.
We explored the notion of types in Chapter 9 and the relationship of classes to types in Chapter 31, but let’s review the basics here so we can see how they apply to metaclasses.
Recall that the type built-in returns the type of any object (which is itself an object). For built-in types like lists, the type of the instance is the built-in list type, but the type of the list type is the type type itself—the type object at the top of the hierarchy creates specific types, and specific types create instances. You can see this for yourself at the interactive prompt. In Python 3.0, for example:
C:\misc> c:\python30\python
>>> type([]) # In 3.0 list is instance of list type
<class 'list'>
>>> type(type([])) # Type of list is type class
<class 'type'>
>>> type(list) # Same, but with type names
<class 'type'>
>>> type(type) # Type of type is type: top of hierarchy
<class 'type'>
As we learned when studying new-style class changes in Chapter 31, the same is generally true in Python 2.6 (and older), but types are not quite the same as classes—type is a unique kind of built-in object that caps the type hierarchy and is used to construct types:
C:\misc> c:\python26\python
>>> type([]) # In 2.6, type is a bit different
<type 'list'>
>>> type(type([]))
<type 'type'>
>>> type(list)
<type 'type'>
>>> type(type)
<type 'type'>
It turns out that the type/instance relationship holds true for classes as well: instances are created from classes, and classes are created from type. In Python 3.0, though, the notion of a “type” is merged with the notion of a “class.” In fact, the two are essentially synonyms—classes are types, and types are classes. That is:
- Types are defined by classes that derive from type.
- User-defined classes are instances of type classes.
- User-defined classes are types that generate instances of their own.
As we saw earlier, this equivalence effects code that tests the type of instances: the type of an instance is the class from which it was generated. It also has implications for the way that classes are created that turn out to be the key to this chapter’s subject. Because classes are normally created from a root type class by default, most programmers don’t need to think about this type/class equivalence. However, it opens up new possibilities for customizing both classes and their instances.
For example, classes in 3.0 (and new-style classes in 2.6) are instances of the type class, and instance objects are instances of their classes; in fact, classes now have a __class__ that links to type, just as an instance has a __class__ that links to the class from which it was made:
C:\misc> c:\python30\python
>>> class C: pass # 3.0 class object (new-style)
...
>>> X = C() # Class instance object
>>> type(X) # Instance is instance of class
<class '__main__.C'>
>>> X.__class__ # Instance's class
<class '__main__.C'>
>>> type(C) # Class is instance of type
<class 'type'>
>>> C.__class__ # Class's class is type
<class 'type'>
Notice especially the last two lines here—classes are instances of the type class, just as normal instances are instances of a class. This works the same for both built-ins and user-defined class types in 3.0. In fact, classes are not really a separate concept at all: they are simply user-defined types, and type itself is defined by a class.
In Python 2.6, things work similarly for new-style classes derived from object, because this enables 3.0 class behavior:
C:\misc> c:\python26\python
>>> class C(object): pass # In 2.6 new-style classes,
... # classes have a class too
>>> X = C()
>>> type(X)
<class '__main__.C'>
>>> type(C)
<type 'type'>
>>> X.__class__
<class '__main__.C'>
>>> C.__class__
<type 'type'>
Classic classes in 2.6 are a bit different, though—because they reflect the class model in older Python releases, they do not have a __class__ link, and like built-in types in 2.6 they are instances of type, not a type class:
C:\misc> c:\python26\python
>>> class C: pass # In 2.6 classic classes,
... # classes have no class themselves
>>> X = C()
>>> type(X)
<type 'instance'>
>>> type(C)
<type 'classobj'>
>>> X.__class__
<class __main__.C at 0x005F85A0>
>>> C.__class__
AttributeError: class C has no attribute '__class__'