"Python" "meta-programming" "Three" "Meta-class"

Source: Internet
Author: User
Tags iterable

# 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

#构建顺序: __prepare__ () __new__ (), __init__ (), Meta class

"Python" "meta-programming" "Three" "Meta-class"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.