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 createdHello
Class 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