The creation of a class
The previous essays are all about classes, creating objects through classes, and how does this class actually come into being?
1. Traditional Creation class
Class Foo (object): def __init__ (self,name): Self.name = name f = Foo ("Shuaigaogao")
F is an object instantiated through the Foo class, in fact, not only F is an object, the Foo class itself is also an object, because in python everything is an object, according to everything is the theory of the object: The Obj object is created by executing the Foo class construction method, Then the Foo class object should also be created by executing the constructor of a class.
Print (Type (f)) #输出: <class ' __main__. Foo ' > : F object created by the Foo class print (Type (Foo)) #输出: <class ' type ' > =: Foo class object created by the type class
So, theF object is an instance of the Foo class, and theFoo class object is an instance of the type class , that is, the Foo class object is created by the type class's construction method
2. Type Create class
Description: type creates the format of the class, class name = Type (' Class name ', (parent class), {' Method name ': Method's memory address})
def func (self): #创建方法 print ("Hello {0}". Format (self.name)) def __init__ (self,name): #创建构造方法 Self.name = Name #通过type创建类, if the classic class is written as: foo = type ("foo", (), {"Talk": Func, "__init__": __init__}) Foo = Type ("foo", ( Object,), {"Talk": Func, "__init__": __init__}) F = Foo ("Shuaigaogao") #创建对象f. Talk () #输出hello Shuaigaogao
Summary: Classes are generated by instantiation of the type class
It is worth noting that the new class of the writing, in the inheritance of the parent class, you inherit a parent class is followed by a comma, plus a comma, it as a tuple, without commas, is a value
__new__ method
1. Concept
The new method is a class-capable method that can be refactored, and the __new__ method executes when instantiated and before the __init__ method.
Class Foo (object): def __init__ (self,name): self.name = name print ("Foo __init__") def __new__ (CLS, * args, **kwargs): print ("Foo __new__", CLS, *args, **kwargs) return object.__new__ (CLS) f = Foo ("Shuaigaogao") # Output foo __new__ <class ' __main__. Foo ' > Shuaigaogao #执行了new方法Foo __init__ #执行了__init__方法
2. New Method function
Action: All objects are instantiated with the new method, and new calls the Init method, so the new method is executed first in the instantiation process, not the Init method.
① Reconstruction __new__ Method
Class Foo (object): def __init__ (self,name): self.name = name print ("Foo __init__") def __new__ (CLS , *args, **kwargs): print ("foo __new__", CLS, *args, **kwargs) F = Foo ("Shuaigaogao") #实例化 #输出Foo __new__ <cl The "__main__". Foo ' > Shuaigaogao
As seen in the above example, the __init__ method is not executed
② Refactor the __new__ method and inherit the __new__ method of the parent class
Class Foo (object): def __init__ (self,name): self.name = name print ("Foo __init__") def __new__ (CLS, * args, **kwargs): #cls相当于传入类Foo print ("Foo __new__", CLS, *args, **kwargs) return object.__new__ (CLS) #继承父类的__new__方法, this side must inherit the form of the return value F = Foo ("Shuaigaogao") #输出Foo __new__ <class ' __main__. Foo ' > Shuaigaogao
From above it is not difficult to see, in most cases, you do not go to refactor your __new__ method, because your parent class already has the __new__ method, has helped you to write how to create the class, if you rewrite, will overwrite the parent class inside the __new__ method. But your refactoring can add a little bit of functionality, but you'll still need to inherit the parent class back if you overwrite it, or you won't be able to create that strength.
3. Usage Scenarios
I want to customize some of my own classes, just before it is instantiated, you can use the __new__ method, the new method is used to create the strength, the refactoring new method, must inherit the parent class's new method in the form of the return value.
① Requirements: When I create an object, I create a class variable at the same time
Class Foo (object): def __init__ (self,name): self.name = name print ("Foo __init__") def __new__ (CLS, * args, **kwargs): #cls相当于是传入的类名Foo cls.name = "Shuaigaogao" #创建对象是定义静态变量 print (cls.name) Return object.__new__ (CLS) #继承父类的__new__方法 f = Foo ("Shuaigaogao") print (foo.name) #输出shuaigaogaoFoo __init__ Shuaigaogao
__metaclass__ method
Metaclass This attribute is called a meta-class, it is used to indicate who the class is to help him instantiate the creation, plainly, is equivalent to customizing a class.
Class MyType (type): Def __init__ (Self,*args,**kwargs): Print ("MyType __init__", *args,**kwargs) def __call__ (Self, *args, **kwargs): Print ("Mytype __call__", *args, **kwargs) obj = self.__new__ (self) print ("Ob J ", Obj,*args, **kwargs) print (self) self.__init__ (Obj,*args, **kwargs) return obj def __new__ (cl S, *args, **kwargs): Print ("Mytype __new__", *args,**kwargs) return type.__new__ (CLS, *args, **kwargs) class Foo (object,metaclass=mytype): #python3统一用这种 #__metaclass__ = MyType #python2.7 in Def __init__ (self,name): Self.name = name print ("foo __init__") def __new__ (CLS, *args, **kwargs): Print ("foo __new__", CLS, *a RGS, **kwargs) return object.__new__ (CLS) f = Foo ("Shuaigaogao") print ("F", f) Print ("FName", F.name) #输出Mytype __new__ Foo (<class ' object ') {' __new__ ': <function foo.__new__ at 0x0000025ef0efd6a8>, ' __init__ ': <function Foo.__init__ at 0x0000025ef0efd620>, ' __qualname__ ': ' Foo ', ' __module__ ': ' __main__ '}mytype __init__ Foo (<class ' object '), {' __new__ ': & Lt;function foo.__new__ at 0x0000025ef0efd6a8>, ' __init__ ': <function foo.__init__ at 0x0000025EF0EFD620>, ' __ qualname__ ': ' Foo ', ' __module__ ': ' __main__ '}mytype __call__ shuaigaogaofoo __new__ <class ' __main__. Foo ' >obj <__main__. Foo object at 0x0000025ef0f05048> shuaigaogao<class ' __main__. Foo ' >foo __init__f <__main__. Foo object at 0x0000025ef0f05048>fname Shuaigaogao
The creation process is as follows:
More __metaclass__ Knowledge: click
Summarize:
The class's build call order is __new__---__init__--and __call__
"Python"--creation of classes, __new__, __metaclass___