Meta-class correlation (Type & metaclass)

Source: Internet
Author: User

"""
Metaclass Effect:

1) Creation of interception classes

2) Modify the class

3) return the modified class

"""


"""
Why use the Metaclass class instead of the function?

Since __metaclass__ can accept any callable object, why use a class, because it is obviously more complex to use the class? There are several reasons for this:

1) intentions will be clearer. When you read Upperattrmetaclass (type), you know what's going to happen next.

2) You can use OOP programming. A meta-class can inherit from a meta-class, overwriting the method of the parent class. A meta class can even use a meta class.

3) You can organize the code better. When you use the meta-class, it's certainly not a simple scenario like the one I've mentioned above, and it's usually about more complex issues. It can be helpful to which comes multiple methods into a class and make the code easier to read.

4) You can use special methods such as __new__, __init__ and __call__. They can help you with different tasks. Even if you can usually dispose of everything in __new__, some people still feel more comfortable with __init__.

5) Wow, the name of this thing is metaclass, certainly non-angel, I have to be careful!

Why should we use meta-classes?

Now back to our big theme, why are you going to use such an error-prone and obscure feature? Well, generally speaking, you don't use it at all:

"Meta-class is the magic of depth, 99% of users should not worry about it at all." If you want to figure out whether you need to use a meta-class, then you don't need it. Those who actually use the meta-class know very well what they need to do and do not need to explain why they use the meta-class at all. "The leader of the--python world Tim Peters
"""

"""
Knowledge Points:
objects are class-created. When you create an object, the __init__ method inside the class executes automatically. The object () executes the __call__ method of the class.
Class is a type created. When you create a class, the __init__ method of type is automatically executed. Class () Executes the __call__ method that executes the type. After the __call__ method is executed, the call method calls not only the __new__ method of the class, but also the __init__ method of the class.

#No. 0 Step: Execute the __init__ method of type "class is an object of type"classFoo:def __init__(self):Passdef __call__(Self, *args, * *Kwargs):Pass#1th Step: Execute the __call__ method of type#1.1 calls the __new__ method of the Foo class (the object of type), which is used to create the object. #1.2 calls the __init__ method of the Foo class (the object of type), which is used to initialize the object. obj =Foo ()#2nd step: Execute foo def __call__ methodObj ()


"""

Meta-class correlation (Type & metaclass)

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.