Used to represent descriptive information for a class
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =ageif __name__ = = ' __main__ ': p = person (' John ', ') print (p.__doc__)
Run results
Class used to represent the current object
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =ageif __name__ = = ' __main__ ': p = person (' John ', ') print (p.__ CLASS__)
Run results
The person class for the current program
Used to represent the module in which the current action object resides
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =ageif __name__ = = ' __main__ ': p = person (' John ', ') print (p.__module __)
Run results
The instantiated object is followed by parentheses to execute the method
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =age def __call__ (self, *args, **kwargs): print ("Test for Class") if __name__ = = ' __main__ ': p = person (' John ', ') p ()
Run results
If the method is not defined, the object that is instantiated directly calls an error
When you print an object, the return value of the method is printed
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =ageif __name__ = = ' __main__ ': p = person (' John ', ') print (p)
Run
If the __str__ method is not defined, the object's memory address will be returned if it is printed directly.
If there is a __str__ method defined.
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =age def __str__ (self): return "The person Class" if __name__ = = ' __ main__ ': p = person (' John ', ') print (p)
Run
Instead of the memory address of the object, the return value of the __str__ method is printed
Used to view all members of a class or object, expressed in a dictionary format
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): " This class is used to describe the" def __init__ (self,name , age): self.name = name self.age =age def __str__ (self): return "The person Class" if __name__ = = ' __main __ ': p = person (' John ', ') print (person.__dict__) print (p.__dict__)
Run results
Python Class (vii)-Special member Method of class