首页 > 编程语言 > 详细

python学习笔记之反射

时间:2019-06-14 17:06:30      阅读:113      评论:0      收藏:0      [点我收藏+]
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‘)
alex = Teacher()
for k in Teacher.dic:
print(k)
key = input(‘输入需求 :‘)
print(Teacher.dic[key])
if hasattr(alex,Teacher.dic[key]):
func = getattr(alex,Teacher.dic[key])
func()
# alex.show_student() ‘show_student‘
func = getattr(alex,‘show_student‘)
func()


if hasattr(Teacher,‘dic‘):
ret = getattr(Teacher,‘dic‘) # Teacher.dic # 类也是对象
# ret2 = getattr(Teacher,‘func‘) # 类.方法 teacher.func
# ret2()
print(ret)


menu = Teacher.dic
for k in menu:
print(k)

# # 通过反射
# # 对象名 获取对象属性 和 普通方法
# # 类名 获取静态属性 和类方法 和 静态方法
#
# # 普通方法 self
# # 静态方法 @staticmethod
# # 类方法 @classmethod
# # 属性方法 @property

python学习笔记之反射

原文:https://www.cnblogs.com/CoolClare/p/11024274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!