1) The reflection function in Python is provided by the following four built-in functions: Hasattr, GetAttr, SetAttr, Delattr,
Four functions are used for internal execution of objects: Check for a member, get a member, set a member, delete a member.
#-*-coding:utf-8-*-__author__='Shisanjun'classFoo (object):def __init__(self): Self.name="s" deffunc (self):return 'func'obj=Foo ()#Check if a member is includedPrint("----Whether there are members-----")Print(Hasattr (obj,"name"))Print(Hasattr (obj,"func"))#Get MembersPrint("----GET Members-----")Print(GetAttr (obj,"name"))Print(GetAttr (obj,"func")())#memory address of Func for GetAttr (obj, "func") instance#Set memberPrint("----SET member-----")defShow (num):returnNum+1setattr (obj," Age", 18) setattr (obj,"Show", 3)Print(GetAttr (obj," Age"))Print(GetAttr (obj,"Show")) delattr (obj," Age") delattr (obj,'Show')Print("----Whether there are members-----")Print(Hasattr (obj," Age"))Print(Hasattr (obj,"Show"))"""----If there are members-----truetrue----GET members-----SFUNC----SET member-----183----Whether there are members-----Falsefalse"""
2) Other methods to access members of the class
class Foo (object): def __init__ (self): ' Alex ' def func (self): return ' func ' # do not allow obj.nameobj = Foo ()print obj.__dict__['name ']
Python Basic Learning Log reflection of class day7-