Python实现使用dir获取类的方法列表


Posted in Python onDecember 24, 2019

使用Python的内置方法dir,可以范围一个模块中定义的名字的列表。

官方解释是:

Docstring:
dir([object]) -> list of strings

If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
 for a module object: the module's attributes.
 for a class object: its attributes, and recursively the attributes
  of its bases.
 for any other object: its attributes, its class's attributes, and
  recursively the attributes of its class's base classes.

通过dir方法,我们可以在一个类的内部,获取当前类的名字满足某些特征的所有方法。

下面是一个例子:

class A(object):
  def A_X_1(self):
    pass

  def A_X_2(self):
    pass

  def A_X_3(self):
    pass

  def get_A_X_methods(self):
    return filter(lambda x: x.startswith('A_X') and callable(getattr(self,x)), dir(self))

执行:

print A().get_A_X_methods()

输出结果为:

> ['A_X_1', 'A_X_2', 'A_X_3']

以上这篇Python实现使用dir获取类的方法列表就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现测试磁盘性能的方法
Mar 12 Python
python判断一个集合是否包含了另外一个集合中所有项的方法
Jun 30 Python
python kmeans聚类简单介绍和实现代码
Feb 23 Python
Python 读取指定文件夹下的所有图像方法
Apr 27 Python
django使用admin站点上传图片的实例
Jul 28 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
Feb 25 Python
python对接ihuyi实现短信验证码发送
May 10 Python
Python docutils文档编译过程方法解析
Jun 23 Python
Django中F函数的使用示例代码详解
Jul 06 Python
深入了解Python装饰器的高级用法
Aug 13 Python
浅析Python 责任链设计模式
Sep 11 Python
Python实战之用tkinter库做一个鼠标模拟点击器
Apr 27 Python
django数据模型on_delete, db_constraint的使用详解
Dec 24 #Python
Python中filter与lambda的结合使用详解
Dec 24 #Python
节日快乐! Python画一棵圣诞树送给你
Dec 24 #Python
Python 3 使用Pillow生成漂亮的分形树图片
Dec 24 #Python
python保存log日志,实现用log日志画图
Dec 24 #Python
Django 限制访问频率的思路详解
Dec 24 #Python
python 统计文件中的字符串数目示例
Dec 24 #Python
You might like
PHP输入流php://input介绍
2012/09/18 PHP
php使用pack处理二进制文件的方法
2014/07/03 PHP
使用PHP把HTML生成PDF文件的几个开源项目介绍
2014/11/17 PHP
php利用fsockopen GET/POST提交表单及上传文件
2017/05/22 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
十个优秀的Ajax/Javascript实例网站收集
2010/03/31 Javascript
js更优雅的兼容
2010/08/12 Javascript
基于datagrid框架的查询
2013/04/08 Javascript
Javascript全局变量var与不var的区别深入解析
2013/12/09 Javascript
关于jQuery判断元素是否存在的问题示例探讨
2014/07/21 Javascript
利用原生JavaScript获取元素样式只是获取而已
2014/10/08 Javascript
js中iframe调用父页面的方法
2014/10/30 Javascript
14个有用的Jquery技巧分享
2015/01/08 Javascript
BootStrap树状图显示功能
2016/11/24 Javascript
javascript表单正则应用
2017/02/04 Javascript
利用ES6语法重构React组件详解
2017/03/02 Javascript
vue.js前后端数据交互之提交数据操作详解
2018/04/24 Javascript
Taro集成Redux快速上手的方法示例
2018/06/21 Javascript
vue.js图片转Base64上传图片并预览的实现方法
2018/08/02 Javascript
浅谈vue方法内的方法使用this的问题
2018/09/15 Javascript
原生js实现的观察者和订阅者模式简单示例
2020/04/18 Javascript
[49:15]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第二场 1月19日
2021/03/11 DOTA
[04:59]DOTA2-DPC中国联赛 正赛 Ehome vs iG 选手采访
2021/03/11 DOTA
Python守护进程用法实例分析
2015/06/04 Python
python中import学习备忘笔记
2017/01/24 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
python抓取多种类型的页面方法实例
2019/11/20 Python
python飞机大战pygame碰撞检测实现方法分析
2019/12/17 Python
美国第二大连锁书店:Books-A-Million
2017/12/28 全球购物
YSL圣罗兰美妆官方旗舰店:购买YSL口红
2018/04/16 全球购物
Saks Fifth Avenue澳洲/亚太地区:萨克斯第五大道精品百货店
2019/06/09 全球购物
幼儿园长自我鉴定
2013/10/17 职场文书
护理专业大学生自我推荐信
2014/01/25 职场文书
2014春晚主持词
2014/03/25 职场文书
干货分享:推荐信写作技巧!
2019/06/21 职场文书
解析目标检测之IoU
2021/06/26 Python