Preliminary understanding of meta-classes
#本篇文章介绍的元类, as well as type Python3 the above version.
I. Preface to the meta-class.
In Python, everything is the object, and of course, the class is an object.
Class C1:
Pass
Obj1 = C1 ()
As you can see from the above example, Obj1 is an object created by the C1 class, and Obj1 is produced by C1, and if the class is an object by the previous theory, who created the C1?
#type函数可以查看类型 can also be used to view the object's classes, both of which are the same
Print (Type (obj1)) # output: <class ' __main__.c1 ' > Representation, Obj1 object created by C1 class
Print (Type (c1)) # Output: <type ' type ' >
That is to say, all of the classes we define are generated by type.
With this knowledge point, you can elicit two ways to create a class.
Method One: Define a class using the class keyword.
Class Foo:
def func (self):
Print (' from Func ')
Method Two: Define a class by type.
def func (self):
Print (' from Func ')
X=1
Foo=type (' Foo ', (object,), {' Func ': Func, ' X ': 1})
Two. What is a meta-class?
The so-called meta-class, which can be understood as a class class, is to control how a class is created, just as the class is the template of the object, and the meta-class is the template of the class.
What is the relationship between the type and the meta-class?
When a class does not declare its own meta-class, the class's default meta-class is type.
More than that, the user can also define a meta-class by type.
Attention! When you customize a meta-class, your own defined meta-class must inherit type!
Anatomy of a meta-class parameter:
Python2.7ver:
Class MyType (Type):
def __init__ (self,class_name,bases,dict):
Print "MyType __init__ runing!!"
The name of the print Class_name # class
Print Bases # Inherited parent class
The namespace dictionary of the Print Dict # class (that is, the __dict__ of the Class)
Print self #定义的类的本体 (that is, the memory address of the class)
Class Test_class (object): # MyType as a meta-class, create a class named Test_class
__metaclass__ = method that specifies a meta-class in MyType #python2.7.
def running (self):
Print "Runing ..."
Next, execute the following code.
Output:
MyType __init__ runing!!
Test_class
(<type ' object ';,)
{' __module__ ': ' test1 ', ' __metaclass__ ': <class ' test1.mytype ';, ' running ': <function running at 0X10E149C08 }
<class ' Test1.test_class ' >
Python3.5ver:
Class MyType (Type):
def __init__ (self,class_name,bases,dict):
Print ("MyType __init__ runing!!")
The name of the print (class_name) # class
Print (bases) # Inherited parent class
The namespace Dictionary of the Print (DICT) # class (that is, the __dict__ of the Class)
Print (self) #定义的类的本体 (that is, the memory address of the class)
Class Test_class (Metaclass = "MyType"): # MyType as a meta-class, creating a class named Test_class
def running (self):
Print ("runing ...")
# from the output we can see that when we create a class, we trigger the __init__ constructor of the meta-class, as if it were the process of creating an object with a class.
How does a class instantiate a property? This function is implemented by using the meta-class manually.
Python2.7ver:
Class MyType (Type):
def __init__ (self,class_name,bases,dict):
Print "MyType __init__ runing!!"
Print Class_name
Print Bases
Print Dict
Print Self
def __call__ (self, *args, **kwargs): # When you use a class to instantiate an object, you need to add () after the class, which triggers the __call__ method defined in the meta-class.
Print "MyType call runing----->%s,%s,%s"% (Self,args,kwargs)
Obje = self.__new__ (self) # generates an empty object
Self.__init__ (Obje,*args,**kwargs) # invokes the constructor of the class created by the meta-class.
Return Obje # Returns an empty object
Class Test_class (object):
__metaclass__ = MyType
A = 1
def running (self):
Print "Runing ..."
Obj1 = Test_class ()
Print obj1.a
This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1919559
8.python Face Object Part.9 (primitive class Part.1)