Python Python's exercise of reflection reflective practice

Copy the code
# What is reflection? String can manner to access properties of an object 
class the Test (): 
    the _name = " sss " 
    DEF Fun (Self):
         return  " HelloWord " 

T = the Test ()
 # Print (the hasattr (T, "_ name")) #hasattr (obj, name) # View class which has no name attribute 
# Print (hasattr (t, "Fun")) #True 

IF hasattr (t, " _name " ):
     Print (getattr (t, " _name " ))    # sss 
IF hasattr (t, " Fun " ):
    (getattr (t, " Fun " ) ())   # HelloWord 
    IF   not hasattr (t, " Age " ):   # If the property does not exist 
        Print ( " Without the properties and methods, I come to set up a " ) 
        setattr (t, " Age " , " 18 is " )   # to set a default value t objects, default = 18 is Age 
        Print (getattr (t, " Age " ))
Copy the code

 

Copy the code
# What is reflection? String can manner to access properties of an object 
class the Test (): 
    the _name = " sss " 
    DEF Fun (Self):
         return  " HelloWord " 

T = the Test ()
 # Print (the hasattr (T, "_ name")) #hasattr (obj, name) # View class which has no name attribute 
# Print (hasattr (t, "Fun")) #True 

IF hasattr (t, " _name " ):
     Print (getattr (t, " _name " ))    # sss 
IF hasattr (t, " Fun " ):
    (getattr (t, " Fun " ) ())   # HelloWord 
    IF   not hasattr (t, " Age " ):   # If the property does not exist 
        Print ( " Without the properties and methods, I come to set up a " ) 
        setattr (t, " Age " , " 18 is " )   # to set a default value t objects, default = 18 is Age 
        Print (getattr (t, " Age " ))
Copy the code

 

Guess you like

Origin www.cnblogs.com/maaosheng/p/11618784.html