python学习:反射的四种基本方法,
python学习:反射的四种基本方法,
class Person(object):
def __init__(self):
pass
def info(self):
print('我是person类中的info方法')
1.getattr()方法
这个方法是根据字符串去某个模块中寻找方法
instantiation = reflect.Person()#先实例化
f = getattr(instantiation,'info')#使用getattr函数去寻找字符串的同名方法
f()#调用方法
#输出结果:我是person类中的info方法
2.hasattr()方法
这个方法是根据字符串去判断某个模块中该方法是否存在
instantiation = reflect.Person()#先实例化
f = hasattr(instantiation,'info')
print(f)
#输出结果:True
3.setattr()方法
这个方法是根据字符串去某个模块中设置方法
instantiation = reflect.Person()
f = setattr(instantiation,'exit','this is a exit method')
f2 = hasattr(instantiation,'exit')
print(f2)
#python学习交流群:153708845
#输出结果就是True
4.delattr()方法
这个方法是根据字符串去某个模块中删除方法
instantiation = reflect.Person()#实例化
f = delattr(instantiation,'exit')
f = hasattr(instantiation,'exit')
print(f)
#输出结果就是False
相关内容
- 这个Python读取文件的方法,堪称天花板级别...,—>
- Jupyter Notebook单元格加宽的方法3种,(一)只改变当前
- Python学习:绑定方法与非绑定方法,在类中正常定义的
- Python中class内置方法__init__与__new__作用与区别探究,P
- Python学习:len() 函数详解:获取字符串长度或字节数、
- form表单内容序列化的两种方法,格式:vardata
- Web的8种(6+2)元素+常用定位方法,find_eleme
- django中的request对象方法,2.request对
- 这个Python读取文件的方法,堪称天花板级别...,说到
- Python学习:数据类型转换的方法教程,示例:>>&
评论关闭