Python day 9 (4) enumeration and metadata, python metadata

Source: Internet
Author: User
Tags define function

Python day 9 (4) enumeration and metadata, python metadata

I. Enumeration

See the great god link: http://www.cnblogs.com/ucos/p/5896861.html

Ii. Metadata

The biggest difference between a dynamic language and a static language is the definition of functions and classes, instead of at compile time, but dynamic creation at runtime.

1 type ()

type()A function can return the type of an object and create a new type. For exampletype()Function createdHelloClass without passingclass Hello(object)...Definition:

1 >>> def fn (self, name = 'World'): # first define function 2... print ('hello, % s. '% name) 3... 4 >>> Hello = type ('hello', (object,), dict (Hello = fn) # create hello class 5 >>> h = Hello () 6 >>> h. hello () 7 Hello, world. 8 >>> print (type (Hello) 9 <class 'type'> 10 >>> print (type (h) 11 <class '_ main __. hello '>

 

To create a class object,type()Function input three parameters in sequence:

Passtype()The class created by the function is exactly the same as writing the class directly, because when the Python interpreter encounters the class definition, it only scans the class definition syntax and then callstype()Function to create a class.

Normally, we useclass Xxx...Class,,type()The function also allows us to dynamically create classes, that is, the dynamic language itself supports dynamic class creation at runtime.

2 metaclass Magic Code

Besidestype()In addition to the dynamic creation class, you can also use metaclass to control the creation behavior of the class.

After defining a class, you can create an instance based on this class. Therefore, you must first define the class and then create an instance.

But what if we want to create a class? You must create a Class Based on metaclass. Therefore, you must first define metaclass and then create a class.

To connect, You can first define metaclass to create a class and then create an instance.

Therefore, metaclass allows you to create or modify classes. In other words, you can think of a class as an "instance" created by metaclass ".

Link: https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python

Liao Shen link: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319106919344c4ef8b1e04c48778bb45796e0335839000

 

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.