类的反射 对象的反射 模块的反射

#什么是反射?
#用字符串数据类型的变量名来访问这个变量的值

#————————类的反射
class Student:
    Money=0
    @classmethod
    def check_course(cls):
        print('查看课程了')
    @staticmethod
    def login():
        print('登录成功')
#反射查看属性
print(getattr(Student,'Money'))
#反射调用方法
getattr(Student,'check_course')()
#反射调用静态方法

if hasattr(Student,'login'):
    getattr(Student, 'login')()
class Person:
    def __init__(self,name):
        self.name=name
    def func(self):
        print('in func')
p1=Person('小吴')
print(getattr(p1,'name'))
getattr(p1,'func')()
原文地址:https://www.cnblogs.com/long-holiday/p/9902300.html