Objective
Meta-class is a very important part of Python, I will be two times to explain the meta-class and its application, this is a meta-class and its application of the first, the following to start today's description ~ ~ ~
1. Classes are also objects
In most programming languages, A class is a group, which describes the code snippet for an object. in Python this point still becomes?:
class Objectcreator (object): ... Pass >>>my_object =>>>print(my_object)<__main__. Objectcreator Object at 0x8974f2c>
However, the classes in Python are far from being so. class is also a kind of object . Yes, yes, that's the object. As long as you make the keyword Class,python interpreter, you create an object when you are practicing.
The following code snippet:
class Objectcreator (object): ... Pass ...
Will create an object in memory, the name is Objectcreator. This object (class object Objectcreator) has the ability to create objects (instance objects). However, its nature is still an object, so you can do the following for it:
1. You can assign it to a variable
2. Can you cuff it?
3. You can add properties to it
4. You can pass it as a function parameter in the
Next? is an example:
>>>Print(Objectcreator)#You can print a class, because it's actually an object.<class '__main__. Objectcreator'> >>>defEcho (o): ...Print(o) ...>>> Echo (Objectcreator)#you can pass a class as a parameter to a function<class '__main__. Objectcreator'> >>>Print(Hasattr (Objectcreator,'New_attribute')) False>>>objectcreator.new_attribute='Foo'#you can add properties to a class>>>Print(Hasattr (Objectcreator,'New_attribute')) True>>>Print(objectcreator.new_attribute) Foo>>>objectcreatormirror=objectcreator#You can assign a class value to a variable.>>>Print(Objectcreatormirror ())<__main__. Objectcreator Object at 0x8997b4c>
2. Dynamically Creating classes
Because classes are also objects, you can create them dynamically at the time of shipment, just like any other object? First, you can create a class in the function so that the? Class keyword can be used.
>>>defChoose_class (name): ...:ifName = ='Foo':...: classFoo (object): ...:Pass...: returnFoo#returns a class, not an instance of a class...:Else:...: classBar (object): ...:Pass...: returnBar ...:>>> Myclass=choose_class ('Foo')>>>Print(MyClass)#The function returns a class, not an instance of the class<class '__main__.choose_class.<locals>. Foo'>>>>Print(MyClass ())#you can create a class instance from this class, that is, the object<__main__. Choose_class.<locals>. Foo Object at 0x000001c54f7007b8>
But this is not dynamic enough, because you still need?? Write code for the entire class. Because classes are also objects, they must be made by what East? when you make the "class" keyword, the Python interpreter creates the object . But just like in Python, most things, Python is still available to you? Method of handling.
Do you remember the built-in function type? This ancient, but strong, function allows you to know what the type of object is, like this:
>>>Print(Type (1))#type of numeric value<class 'int'>>>>Print(Type ("1"))#type of String<class 'Str'>>>>Print(Type (Objectcreator ()))#type of instance object<class '__main__. Objectcreator'> >>>Print(Type (objectcreator))#type of Class<type'type'>
Look closely at the results of the operation, found that the type of the Objectcreator view is, the answer is type, is not a little surprised ... Take a look?
3. Create a class with the? type
The type also has a completely different function, dynamically creating the class.
Type can accept the description of a class as a parameter, and then return to the class. (To be sure, depending on the parameters passed in, the same function has two completely different types of laws. It's a silly thing, but it's in Python for backwards compatibility)
Type can be like this?:
Type (class name, tuple that consists of the. class name, which can be empty for inheritance), contains the dictionary (name and value) of the property
? The following code:
class Test:# defines a test class ... : pass ...: in [8]: Test ()# created? An instance object of the test class Out[8]: <__main__. Test at 0x1c54f648518>
You can create a motion like this:
In [9]: Test2 = Type ("Test2", (), {})# defines a Test2 class in [ x]: Test2 ()# created an instance object of the Test2 class out[10]: <__main__. Test2 at 0x1c54f66ed68>
We make? " Test2 "As the class name, and it can also be used as a variable to the class of the citation? Classes and variables are different, and there's no reason to complicate things. That is, the 1th argument in the type function, which can also be called the other name, which indicates the name of the class.
In [all]: Mydogclass = Type ('mydog', (), {}) in [print(mydogclass) <class'__main__. Mydog'>
Make Help to test these 2 classes
in [+]: Help (Test) # ?help View Test class Help on class Test __main__ : class Test (builtins.object) | Data descriptors defined here: | | __dict__ | Dictionary for instance variables (if Span style= "color: #000000;" > defined) | | __weakref__ | List of weak references to the object (if defined)
in [+]: Help (Test2)#. Help view Test2 class in__main__: Class| | | __dict__ | for instance variables (if| | __weakref__ | List of weak references to the object (if defined)
4. Create a class with an attribute in the. Type
Type accepts a dictionary to define properties for the class, so
in [+]: foo = type ('foo', (), {'bar': True})
Can be translated as:
class Foo (object): ...: = True
And can you think of Foo as an ordinary class?:
Print (Foo)<class'__main__. Foo'> in[print(foo.bar) Truein [+]: F = Foo () in [print (f)<__main__. Foo object at 0x000001c54f6511d0> in[print(f.bar) True
Of course, you can inherit from this class, so the following code:
class Foochild (Foo): ...: Pass
Can be written as:
In [all]: Foochild = Type ('foochild', (Foo,), {}) in [print(foochild) <class'__main__. Foochild'> in[print(foochild.bar) True
Attention:
Python advanced detailed meta-class and its Application 1