Python meta-class analysis

Source: Internet
Author: User

It's a struggle to start touching the concept of the meta-class in Python's new class. I don't know what this is.

Use the following definitions to illustrate:

(1) In Python, the class is also an object. It's just a special kind of object that he uses to create other objects.

(2) The Meta-class is also a class, but it is more special ... He is the class used to create other classes ... (hehe, is not very mouthful)


Let's take a look at some code:

Class Fjs (object):    def __init__ (self, name):        self.name = name    def hello (self):        print ' hello by%s '% (sel F.name,) Print fjs.__class__

The output here is: <type ' type ' >

How do you understand it here? According to the above-mentioned ... The Fjs class is actually an object ... So what class is the Fjs class object created by? Well.. is created by the type class ...

Next, change the code to create the FJS type in a different way:

def __init__ (self, name):    self.name = namedef Hello (self):    print "Hello by%s"% (Self.name,) attrs = Dict () attrs[" __init__ "] = __init__attrs[" Hello "] = Hellofjs = Type (" Fjs ", (object,), attrs) fjs = Fjs (" Fjs ") Fjs.hello ()

The FJS type is also created here, the same as the FJS type defined above. But here's a different way of defining ...

I can see what this class is for, right here.


Well, then introduce the concept of meta-class ....

In Python, the definition of a type (a modern Class) is ultimately created by a meta-class to create a type object .... Under normal circumstances, if not specifically stated ... Then the default meta-class is type ... Through the above mentioned two code, should be able to understand the problem clearly ...


Now let's write the code to see the custom meta-class:

Class Meta_fjs (Type):    def __init__ (self, name, parents, attrs):        print "Start creating type Object"        super (Meta_fjs, self). __ INIT__ (name, parents, Attrs) class Fjs (object):    __metaclass__ = Meta_fjs    def __init__ (self, name):        Self.name = name    def hello (self):        print ' hello by%s '% (Self.name,)

This defines a META_FJS meta-class, which inherits the type, and then the thing to do, in fact, is simply to call type to create the type object.

In the defined FJS type, we specify the __metaclass__ property of this class as the Meta_fjs meta class just created ...

In this way, when creating the FJS type, it is actually created by meta_fjs ....


Of course there's nothing to do here ... Then do something slightly useful:

Class Meta_fjs (Type):    def __init__ (self, name, parents, attrs):        If "__str__" isn't in Attrs:            raise TypeError (" No defined __str__ method ")        super (Meta_fjs, self). __INIT__ (name, parents, Attrs) class Fjs (object):    __metaclass__ = Meta_ FJS    def __init__ (self, name):        self.name = name    def hello (self):        print ' hello by%s '% (Self.name,)

Here, when creating the type, it is required to have the __str__ method, if not, then will throw an exception, then at run time, the next definition of the FJS type will throw an exception. If you want to pass smoothly, you need to add the __str__ method to the definition of FJS.


So far, some of the general concepts of Python's meta-class should be known ...


In fact, in general we will not use the meta-class related things ... However, in some framework design, API design and other places in the meta-class can also play an important role in ...


Python meta-class analysis

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.