Online Book Reader

Home Category

Learning Python - Mark Lutz [566]

By Root 1926 0
our classes? It turns out that classes are instances of something, too:

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

>>> type(type([])) # Type of list is type class

>>> type(list) # Same, but with type names

>>> type(type) # Type of type is type: top of hierarchy

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(type([]))

>>> type(list)

>>> 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

>>> X.__class__ # Instance's class

>>> type(C) # Class is instance of type

>>> C.__class__ # Class's class is 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)

>>> type(C)

>>> X.__class__

>>> C.__class__

Return Main Page Previous Page Next Page

®Online Book Reader