反射
什么是反射?
通过字符串的形式操作对象相关属性。python中的事物都是对象;
关键方法:
(1)getattr:获取属性
(2)setattr:设置属性
(3)hashattr:检测是否含有属性,返回布尔值
(4)delattr:删除属性
具体功能演示:
class Foo: f = '类对静态变量' def __init__(self, name, age): self.name = name self.age = age def say_hi(self): print('hi, %s' % self.name) obj = Foo('xiaofei', 20) # 检测是否含有某属性 print(hasattr(obj, 'name')) print(hasattr(obj, 'say_hi')) # 执行结果: # True # True print('--------------------------------------') # 获取属性: print(getattr(obj, 'name')) func = getattr(obj, 'say_hi') func() print(getattr(obj, 'aaaaa', '不存在!')) # 执行结果: # xiaofei # hi, xiaofei # 不存在! print('--------------------------------------') # 设置属性 setattr(obj, 'gender', 'female') setattr(obj, 'show_name', lambda self: self.name + 'er') print(obj.gender) print(obj.show_name(obj)) # 执行结果: # female # xiaofeier print('--------------------------------------') # 删除属性 delattr(obj, 'age') delattr(obj, 'show_name') print(obj.__dict__) # 执行结果: # {'gender': 'female', 'name': 'xiaofei'} delattr(obj, 'aaaa') # 删除不存在对属性,会报错 实例中的用法: import os class Manage_cmd(object): def run(self): while True: cmd = input('>>>').strip() if not cmd: continue if hasattr(self, cmd): func = getattr(self, cmd) func() else: print('-bash: %s: command not found' % cmd) def ls(self): print(os.listdir('.')) cmd = Manage_cmd() cmd.run() # 执行结果: # >>>ls # ['test1.py'] # >>>asdfadf # -bash: asdfadf: command not found
1. 反射不仅仅只能用在属性上,也可以用在方法上
class Foo(object): staticField = 'hello world.' def __init__(self): self.name = 'hkey' def test(self): return 'test' @staticmethod def bar(): return 'bar' print(getattr(Foo, 'staticField')) print(getattr(Foo, 'test')) func = getattr(Foo, 'bar') print(func()) # 执行结果: # hello world. # <function Foo.test at 0x00000141FE18D158> # bar
2. 反射当前文件(模块)成员
import sys def s1(): print('s1') def s2(): print('s2') this_module = sys.modules[__name__] print(hasattr(this_module, 's1')) func = getattr(this_module, 's2') func() # 执行结果: # True # s2
3. 导入其他模块,利用反射查找该模块是否存在某个方法
module_test.py #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: hkey def test(): print('form the test.') test1.py ''' 程序目录: module_test.py test1.py 当前文件: test1.py ''' import module_test as obj if hasattr(obj, 'test'): func = getattr(obj, 'test') func() # 执行结果: # form the test.
item 系列
关键方法:
__getitem__:如果在类中定义了__getitem__()方法,那么就可以通过dict的形式,P[key]取值,当实例对象作P[key]运算时,就会调用类中的__getitem__()方法;
class Student(object): def __init__(self, name): self.name = name def __getitem__(self, item): print(self.__dict__[item]) s = Student('hkey') s.age = 20 s['name'] s['age'] # 执行结果: # hkey # 20
__setitem__:如果在类中定义了__setitem__()方法,那么就可以通过dict的形式添加属性和方法;
class Student(object): def __init__(self, name): self.name = name def __setitem__(self, key, value): self.__dict__[key] = value def __getitem__(self, item): print(self.__dict__[item]) s = Student('hkey') # 调用类的__setitem__方法,通过dict的形式进行添加对象的属性 s['age'] = 20 s['hobby'] = 'coding' # 调用类的__getitem__方法,通过dict的形式获取对象的属性 s['age'] s['hobby'] # 执行结果: # 20
__delitem__:通过dict的形式,删除对象的属性或方法
class Student(object): def __init__(self, name): self.name = name def __getitem__(self, item): return self.__dict__[item] def __delitem__(self, key): self.__dict__.pop(key) print('执行我了。') s = Student('hkey') s.age = 20 print(s['name']) print(s['age']) del s['age'] # 执行结果: # hkey # 20 # 执行我了。