小猿圈python学习-神奇的反射

106 阅读2分钟
    self.age = age
def say_hi(self):
    print("hi,guys , my name is " ,self.name)

obj=Person('Alex',26) #检测是否含有某属性 print(hasattr(obj,'name')) print(hasattr(obj,'say_hi')) #获取属性 n=getattr(obj,'name') print(n) func=getattr(obj,'say_hi') func() print(getattr(obj,'aaaaaaaa','不存在啊')) #报错 #设置属性 setattr(obj,'hobbie',"girl") setattr(obj,'show_name',lambda self:self.name+'--%s' % self.age) print(obj.dict) print(obj.show_name(obj)) #删除属性 delattr(obj,'age') delattr(obj,'show_name') #delattr(obj,'show_name111') # 不存在,则报错 print(obj.dict) 反射模块成员 除了可以用来检测类中有没有某个方法,还可以用来检测模块下有没有方法、类、或者变量

def s1(): print('s1') class Person(object): def init(self,name,age): self.name = name self.age = age name = "test" this_module = sys.modules[name] # name 会动态的代表当前模块名 print(hasattr(this_module, 's1')) print(hasattr(this_module, 'name')) print(getattr(this_module, 'Person')) p = getattr(this_module, 'Person') p("Alex",22) 反射从其它模块导入的方法

反射的应用 了解了反射的四个函数。那么反射到底有什么用呢?它的应用场景是什么呢?

现在让我们打开浏览器,访问一个网站,你单击登录就跳转到登录界面,你单击注册就跳转到注册界面,等等,其实你单击的其实是一个个的链接,每一个链接都会有一个函数或者方法来处理。

没学反射之前的解决方式

class User: def login(self): print('欢迎来到登录页面') def register(self): print('欢迎来到注册页面') def save(self): print('欢迎来到存储页面') while 1: choose = input('>>>').strip() if choose == 'login': obj = User() obj.login() elif choose == 'register': obj = User() obj.register() elif choose == 'save': obj = User() obj.save() 学了反射之后解决方式

class User: def login(self): print('欢迎来到登录页面') def register(self): print('欢迎来到注册页面') def save(self): print('欢迎来到存储页面') user = User() while 1: choose = input('>>>').strip() if hasattr(user,choose): func = getattr(user,choose) func() else: print('输入错误。。。。') 这样就可以明确的感觉到反射的好处

动态加载模块 如果在程序去过过程中,要求程序按用户输入的指令字符串导入对应的模块,在只知道一个模块的字符串名字的情况下,能否导入?

import importlib import('import_lib.metaclass') #这是解释器自己内部用的 #importlib.import_module('import_lib.metaclass') #与上面这句效果一样,官方建议用这个