–
#反射学习 class Teacher(object): dic = {"查看学生":"show_student", "查看老师":"show_teacher"} @classmethod def show_student(self): print("show_student") def show_teacher(self): print("show_teacher") #反射 if hasattr(Teacher, 'dic'): ret = getattr(Teacher, 'dic') print(ret) #获取属性 ret2 = getattr(Teacher,'show_student') ret2() #获取方法调用结果 alex = Teacher() func = getattr(alex,'show_teacher') func() #根据字符串执行对应的方法名 # for k in Teacher.dic: # print(k) alex = Teacher() #key = "查看学生" key = "查看老师" func = getattr(alex, Teacher.dic[key]) func()
–
–
–
评论前必须登录!
注册