Reflection is the State, property, method by which the program runs through a string mapping or modification
There are 4 ways of doing this:
Hasattr ():
Hasattr (object,string): Object instantiated As String
A method or property that determines whether the object has a string in it, returns a Boolean type
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (self): print ('%s are eating '%self.name) if __name__ = = ' __main__ ': p = person (' John ') #实例化Person类
argu = input (">>>:"). Strip () print (Hasattr (P,ARGU))
Run, enter name
Class has the name attribute, which returns true
Input Eat
Class has eat This method, returns True
Enter test
The class does not have the test property and method, which returns false
GetAttr ():
GetAttr (Object,string[,default]): Object is instantiated, string is the default value
Gets the method or property of the object, based on string strings, that returns the value of default if the object does not have a string property or method in it, and if the value of default is not set, an error will be
Gets the method in the class and calls the
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (">>>:"). Strip () if Hasattr (P,ARGU): getattr (P,argu) (' Meat ')
Execution results
Get the value of a property in a class
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (">>>:"). Strip () print (GetAttr (P,ARGU))
If the string is not a property in the class, the specified default value is returned
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (">>>:"). Strip () print (GetAttr (P,argu, "test"))
Enter a, the class does not have a property, then return the default value of the specified test
SetAttr ():
SetAttr (Object,string,value): Object is instantiated, string is string, value is
SetAttr () to set a method or property
A function that sets the passed string as string to the value name, and then invokes the GetAttr ()
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) #在类外定义一个函数def Talk: print ("%s is Talking "%self.name) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (" >>>: "). Strip () SetAttr (p,argu,talk) #把传入的字符串设置为talk函数 getattr (P,argu) (p) #把实例化的对象传入函数中
Run, enter a
Instead of calling the talk () function, the string A that is set to the talk () function
SetAttr () Setting properties
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (">>>:"). Strip () setattr (P,argu, ' man ') #设置属性 print (GetAttr (P,ARGU))
The string passed in, if not a property in the class, creates a new property string for the class and assigns value to the new property
The string passed in, and if it is a property in the class, value overrides the original property
Delattr ():
Delattr (object,string): Remove string Property from Object
#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): def __init__ (self,name): self.name = name def Eat (Self,food): print ('%s is eating%s '% (Self.name,food)) if __name__ = = ' __main__ ': p = person (' John ') Argu = input (">>>:"). Strip () delattr (P,argu) print (GetAttr (P,ARGU))
Run, enter name
Error, the name attribute is not in the person class
Python Class (iv)-reflection