Django - Magic method: def __str __ ()

  1. __ STR __ is python to a method of object base class , i.e. python have all classes ;
  2. Of course django class has the modle ;
  3. We usually create a class of objects, Print the object when will generally be <__ main_ .TestClass Object AT 0x7f4126578828>, that is the object of their class and memory address ;
  4. We rewrite the class __ str __ method after can upon print to get the desired information on easy-to-read person objects , the following are examples
# General 
class the TestClass:
     DEF  the __init__ (Self): 
        the self.name = ' TestCase ' 
T = the TestClass ()
 Print (T)        # give <__ __ main AT 0x7f8c49b21828 the TestClass Object.> 

# Rewriting method __str__ class the TestClass: DEF the __init__ ( Self): the self.name = ' TestCase ' DEF __str__ (Self): return the self.name T = the TestClass () Print (T)        # Get testcase

 




Guess you like

Origin www.cnblogs.com/gengyufei/p/12628883.html