预计阅读本页时间:-
Metaclasses
Most of the changes and feature additions of new-style classes integrate with the notion of subclassable types mentioned earlier in this chapter, because subclassable types and new-style classes were introduced in conjunction with a merging of the type/class dichotomy in Python 2.2 and beyond. As we’ve seen, in 3.0, this merging is complete: classes are now types, and types are classes.
Along with these changes, Python also grew a more coherent protocol for coding metaclasses, which are classes that subclass the type object and intercept class creation calls. As such, they provide a well-defined hook for management and augmentation of class objects. They are also an advanced topic that is optional for most Python programmers, so we’ll postpone further details here. We’ll meet metaclasses briefly later in this chapter in conjunction with class decorators, and we’ll explore them in full detail in Chapter 39, in the final part of this book.