1. _ STR _ and _ repr _ Methods
Class animal: def _ init _ (self, name, age): Self. name = Name self. age = age def F (Self): Print ("this is the method of the parent class") Class persion (animal): Country = "Chinese" def _ init _ (self, name, age, sex): animal. _ init _ (self, name, age) self. sex = sex def eat (Self): Print ("this is the method in the subclass") # def _ STR _ (Self ): # Return "this is str In the subclass" def _ repr _ (Self): Return "this is repr In the subclass" A = persion ("James", 18, "sex ") print (a) print (STR (A) print (Repr (A) print (. _ repr _ () print (. _ STR _ () print ('% s' % A) print (' % R' %)
The two double-bottom methods can change the display of strings and print an object. Essentially, the _ STR _ method is called. If the _ repr _ method is not found, find the _ repr _ method, if no repr is found, the Child preparation of __repr _ is _ STR _ in the parent class, __str _ Is Not _ repr, the parent class has a _ STR _ method. Once called, the memory address of the object that calls this method is returned. There are four methods to call _ STR.
2. The _ del _ destructor makes a final task before deleting an object.
Note: This method is generally not needed to be defined. Because python is a high-level language, programmers do not need to worry about memory allocation and release when using it, because this job is to be executed by the python interpreter, the Destructor is automatically triggered by the interpreter during garbage collection.
Class persion (animal): Country = "Chinese" def _ init _ (self, name, age, sex): animal. _ init _ (self, name, age) self. sex = sex def eat (Self): Print ("this is the method in the subclass") # def _ STR _ (Self ): # Return "this is str In the subclass" def _ repr _ (Self): Return "this is repr In the subclass" def _ del _ (Self ): print ("I want to delete objects") A = persion ("James", 18, "sex") del aprint ()
Del executes this method and also deletes the variable. Execute _ del __and finish the operation before executing the delete operation and deleting an object.
3. The _ call _ object is followed by parentheses for execution: Object () or class ()()
Class Foo (): def _ call _ (self, * ARGs, ** kwargs): Print ("Print call function") A = Foo () () foo ()()
4. _ new _ constructor instantiation object
Before calling the _ init _ method, call the _ new _ method to return an instance self. If the _ new _ method does not return the strength of the current class CLs, the init method will not be called. Because every class instantiation process is controlled by _ new _, we can simply implement instantiation through the _ new _ method.
Instantiate an object through the _ new _ method and open up an address space, __new _ method return instance, the init method will not be called
Class animal: def _ init _ (self, name, age): Self. name = Name self. age = age def F (Self): Print ("this is the method of the parent class") Class persion (animal ): country = "Chinese" _ instance = false def _ init _ (self, name, age, sex): animal. _ init _ (self, name, age) self. sex = sex print ("init method") def eat (Self): Print ("this is the method in the subclass") def _ new _ (CLS, * ARGs, ** kwargs): If Cls. _ instance: Return Cls. _ instance Cls. _ instance = object. _ new _ (persion) return Cls. _ instancea = persion ("James", 18, "sex"). cloth = "Little Flower jacket" Print (a) print (. _ dict _) B = persion ("red", 19, "NV") print (B) print (B. _ dict _) print (. _ dict __)
In Singleton mode, only one instance is generated, that is, the first instance. The final strength is equivalent to renaming the attributes of the previous object.
5. The EQ method in the _ eq _ method parent class compares the memory address by default.
Class animal: def _ init _ (self, name, age): Self. name = Name self. age = age def F (Self): Print ("this is the method of the parent class") Class persion (animal): def _ init _ (self, name, age, sex): animal. _ init _ (self, name, age) self. sex = sex def eat (Self): Print ("this is the method in the subclass") def _ eq _ (self, other): If self. name = Other. name: Return true else: Return falsea = persion ("James", 18, "sex") B = persion ("James", 43, "female ") print (A = B)
Compare names to determine if they are equal
Application: 100 student records, some of which have the same name and gender and have different ages. The same name and gender are considered to be the same person.
6. _ delitem _ delattr _ setitem _ getitem __
Class FOO: def _ init _ (self, name): Self. name = Name def _ getitem _ (self, item): Print ("executed getitem method") # print (self. _ dict _ [item]) return none def _ setitem _ (self, key, value): Print ("executed setitem method") self. _ dict _ [Key] = value def _ delitem _ (self, key): Print ('del OBJ [Key], I run ') self. _ dict __. pop (key) def _ delattr _ (self, item): Print ('del obj. key, I execute ') self. _ dict __. pop (item) F1 = Foo ('SB ') f1 ['age'] = 18f1 ['ag1'] = 19del f1.age1del F1 ['age'] F1 ['name'] = 'Alex 'print (F1. _ dict __) print (F1 ["agge"])
Double-bottom method in Object-Oriented