‘‘‘
# str, type, and LineItem are subclasses of Object str, object, and LineItem are instances of type, because they are all classes
The relationship between the object class and the type class is unique: object is an instance of type, and type
is a subclass of object. This relationship is "magical" and cannot be expressed in Python code because it defines one of the
One must exist before the other. Type is an instance of itself and that's amazing.
>>> ' spam '. __class__
<class ' str ' >
>>> str.__class__
<class ' type ' >
>>> from BULKFOOD_V6 import LineItem
>>> lineitem.__class__
<class ' type ' >
>>> type.__class__
<class ' type ' >
‘‘‘
‘‘‘
The class that the Abcmeta eventually belongs to is also type. All classes are directly or indirectly instances of type
However, only the Meta class is also a subclass of type. To understand a meta-class, be sure to know the relationship: a meta-class (such as
Abcmeta) inherits the ability to build classes from the type class
>>> Import Collections
>>> collections. Iterable.__class__
<class ' ABC. Abcmeta ' >
>>> Import ABC
>>> ABC. Abcmeta.__class__
<class ' type ' >
>>> ABC. abcmeta.__mro__
(<class ' ABC. Abcmeta ';, <class ' type ';, <class ' object ' >)
Iterable is a subclass of object and an instance of Abcmeta. Both object and Abcmeta are
An instance of type, but the important relationship here is that Abcmeta is still a subclass of type because Abcmeta is
Meta class. Only iterable are abstract classes.
The point we want to take is that all classes are instances of type, but the Meta class is also a subclass of type, so you can
Factory as a manufacturing class. Specifically, a meta-class can be customized by implementing the __init__ method. Meta-class
The __init__ method can do anything that a class decorator can do, but it is more useful, as the next practice
Shown
‘‘‘
#构建顺序: __prepare__ () __new__ (), __init__ (), Meta class
"Python" "meta-programming" "Three" "Meta-class"