反射

2019-08-26  本文已影响0人  Yanl__

hasattr 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()
上一篇下一篇

猜你喜欢

热点阅读