Python metaclass (Meta Class) __python

Source: Internet
Author: User
Tags python metaclass

The Meta class in Python is the object that creates the class object , which is a bit awkward to say. To understand the Meta class first we need to understand the class, which is a rule for creating objects. A class is also an object in Python. Each object has a type, then the class is of which type.
Let's do the experiment:

Class A:
 pass
a = A ()
a.__class__ #<class ' __main__. A ', using the __class__ property or using the type built-in function to view his type

#既然说类也是对象, let's look at what the class is
a.__class__.__class__ #<class ' type ' >

#我们看看其他类的类型是不是也是type
Type (1). __class__
type (' a '). __class__ #都是type, which indicates that the class of these classes is of type

In the previous article, we talked about several uses of type, including methods for creating class objects. Now that the Meta class is the object that creates the class object. So we're going to create an object that uses the Meta class.

def metafunc (name,parents,attrs):
    print (name,parents,attrs) return
    type (name,parents,attrs)

class A (Metaclass=metafunc): #python3中的写法, use the __metaclass__ property in Python2 to assign A value Meta class pass
A () #A () {' __module__ ': ' __main__ '.....}

It's a process-oriented one, so here's another way to write an object-oriented

Class MC (type): #继承type
  def __new__ (CLS, name, parents, attrs): #重写new方法 return
    type.__new__ (cls,name,parents, ATTRS) #调用父类new方法

Class A (METACLASS=MC):
    pass

A () #这样我们在看A. __class__ will see that his type is <class ' __main__. MC ' >

Here we say the role of the Meta class:
Since the Meta class is the object that creates the class object, it means that the class object has not yet been executed before the Meta class. The role of the Meta class is one way to transform class objects when creating class objects.

The __new__ method above is simple to say,
We all know some of the special methods that classes use when creating objects, and some of the special methods we use in the object declaration cycle are __init__ and __del__. There is a way to __new__ he is a function executed before INIT, its definition __new__ (Cls,params ...) Self to the class object CLS, the remaining params with the init parameter, it must have a return value, and his return value is the instance object itself.

Class A:
    def __init__ (self,name):
        print ("Init", name)
    def __new__ (CLS, name):
        print ("new", name)
        return Super (A,CLS). __new__ (CLS) '
Self will point to the object returned by new
'

Reference: http://blog.jobbole.com/21351/

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.