Python in Meta class

Source: Internet
Author: User

A. Premises:

In Python everything is an object, either a class object or a meta-class object, and the type meta-class is its own object. An inheritance hierarchy is represented as a type (meta-Class)--Class (built-in and user-defined) and an instance of the class.

Two. Purpose:

When you create a class, you automatically change the class, in other words, create something like this.

Three. __metaclass__

You can create a class by specifying __metaclass__ to use a user-defined meta-class.

  Class Statement parsing order :

Class Myclass (object): Pass

MyClass inside of __metaclass__----__metaclass__ in the parent class (object)---The default type in the module

Four. How to use:

1.__metaclass__ = Callablefunc

1 def   Callablefunc (classname, Bases, DCT):2         ... 3         Data Processing 4         ... 5         return  Type (classname, bases, NEWDCT)

2 __metaclass__ = Custom meta-Class

1 class Mymeta (type): 2         def __new__ (CLS, classname, Bases, DCT): 3                 ... 4                 Data processing for DCT, generating NEWDCT5                 ... 6                 return super (MYMETA,CLS). __new__ (CLS,CLASSNAME,BASES,NEWDCT)

  

   

  

Python in 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.