什么是反射?
用字符串数据类型的变量名来访问这个变量的值
反射的应用: 类,对象,模块,反射自己模块的内容
反射的方法:
1.getattr
2.hasattr
3.setattr
4.delattr
类
反射查看属性:
class Student: mind="有思想的" @classmethod def check_course(cls): print("查看课程") @staticmethod def login(): print("登录") print(Student.mind) print(getattr(Student,"mind")) 有思想的 有思想的
反射调用方法:
class Student: mind="有思想的" @classmethod def check_course(cls): print("查看课程") @staticmethod def login(): print("登录") print(getattr(Student,"check_course")) getattr(Student,"check_course")() <bound method Student.check_course of <class '__main__.Student'>> 查看课程
class Student: mind="有思想的" @classmethod def check_course(cls): print("查看课程") @staticmethod def login(): print("登录") num=input(">>>") getattr(Student,num)() 输入:check_course 返回结果:查看课程 输入任意值 返回结果:报错
这时我们就可以进行异常处理了hasatter
class Student: mind="有思想的" @classmethod def check_course(cls): print("查看课程") @staticmethod def login(): print("登录") num=input(">>>") if hasattr(Student,num): getattr(Student,num)()
对象
方法 对象属性
class A(): def __init__(self,name): self.name=name def func(self): print("func....") a=A("asdf") print(a.name) print(getattr(a,"name")) 结果: asdf asdf
class A(): def __init__(self,name): self.name=name def func(self): print("func....") a=A("asdf") print(a.func()) getattr(a,"func")() 结果: func.... func....
模块
import os os.rename("__init__.py",'init') getattr(os,"name")("__init__.py",'init')
注:
getattr(os,"name") == os.rename
import os r=os.rename g=getattr(os,"name") r("__init__.py","init") g("__init__.py","init")
注:
os.rename('__init__.py','init')
getattr(os,"name")('init','init2')
反射自己模块中的内容
class A: print("A...") class B: print("B...") A() B()
执行自己模块的函数只需要A()就可以,但是当前找不到自己当前文件所在的命名空间.
class A: print("A...") class B: print("B...") # import 都相当于导入了一个模块 import sys # import sys 是一个模块,这个模块里的所有的方法都是和python解释器相关的 # print(sys.modules) 结果: A... B... {'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'encodings': <module 'encodings' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\encodings\__init__.py'>, 'codecs': <module 'codecs' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\encodings\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\encodings\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'E:/chenwei/陈伟learning/week5/day1.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\encodings\latin_1.py'>, 'io': <module 'io' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\io.py'>, 'abc': <module 'abc' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site.py'>, 'os': <module 'os' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\os.py'>, 'stat': <module 'stat' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\stat.py'>, '_stat': <module '_stat' (built-in)>, 'ntpath': <module 'ntpath' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ntpath.py'>, 'genericpath': <module 'genericpath' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\genericpath.py'>, 'os.path': <module 'ntpath' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ntpath.py'>, '_collections_abc': <module '_collections_abc' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\_sitebuiltins.py'>, 'sitecustomize': <module 'sitecustomize' from 'D:\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend\sitecustomize.py'>} # sys.modules 这个方法 表示所有在当前这个python程序中导入的模块 # print(sys.modules['__main__']) 结果: A... B... my_file = sys.modules['__main__'] my_file.A() my_file.B() getattr(my_file,'A')() getattr(my_file,'B')() 结果: A... B...
总结:
反射
hasattr,getattr
类名.名字
getattr(类名,'名字')
对象名.名字
getattr(对象,'名字')
模块名.名字
import 模块
getattr(模块,'名字')
自己文件.名字
import sys
getattr(sys.modules['__main__'],'名字')