hasattr getattr delattr
- hasattr
判断该反射是否存在,存在返回True - getattr
通过字符串获得反射 - delattr
class Teacher:
dic = {'查看学生信息':'show_student', '查看老师信息':'show_teacher'}
def show_student(self):
print('show_student')
def show_teacher(self):
print('show_teacher')
@classmethod
def func(cls):
print('hahaha')
zhou = Teacher()
if hasattr(zhou, 'show_student'):
ret = getattr(zhou, 'show_student') # 类名.方法 zhou.shou_student -> getattr(类名, '方法')
ret()
网友评论