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生成密码库功能示例
May 23 Python
Python实现简单过滤文本段的方法
May 24 Python
Python简单计算数组元素平均值的方法示例
Dec 26 Python
selenium+python 去除启动的黑色cmd窗口方法
May 22 Python
python用pandas数据加载、存储与文件格式的实例
Dec 07 Python
pip安装python库的方法总结
Aug 02 Python
Python对接 xray 和微信实现自动告警
Sep 17 Python
Python 用turtle实现用正方形画圆的例子
Nov 21 Python
在tensorflow中设置保存checkpoint的最大数量实例
Jan 21 Python
Python 数据科学 Matplotlib图库详解
Jul 07 Python
yolov5返回坐标的方法实例
Mar 17 Python
python开发制作好看的时钟效果
May 02 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 curl模拟浏览器采集阿里巴巴的实现代码
2011/04/20 PHP
php运行时动态创建函数的方法
2015/03/16 PHP
PHP实现将标点符号正则替换为空格的方法
2017/08/09 PHP
Laravel学习教程之从入口到输出过程详解
2017/08/27 PHP
jQuery实现点击文本框弹出热门标签的提示效果
2013/11/17 Javascript
jsPDF生成pdf后在网页展示实例
2014/01/16 Javascript
Jquery Ajax解析XML数据(同步及异步调用)简单实例
2014/02/12 Javascript
用html5 js实现点击一个按钮达到浏览器全屏效果
2014/05/28 Javascript
JS小游戏之象棋暗棋源码详解
2014/09/25 Javascript
Jquery设置attr的disabled属性控制某行显示或者隐藏
2014/09/25 Javascript
JavaScript中的alert()函数使用技巧详解
2014/12/29 Javascript
自定义百度分享的分享按钮
2015/03/18 Javascript
javascript单页面手势滑屏切换原理详解
2016/03/21 Javascript
详细总结Javascript中的焦点管理
2016/09/17 Javascript
Jquery Easyui表单组件Form使用详解(30)
2016/12/19 Javascript
Bootstrap栅格系统简单实现代码
2017/03/06 Javascript
一篇看懂vuejs的状态管理神器 vuex状态管理模式
2017/04/20 Javascript
详解Angular2表单-模板驱动的表单(Template-Driven Forms)
2017/08/04 Javascript
js前端面试之同步与异步问题详解
2019/04/03 Javascript
JQuery+Bootstrap 自定义全屏Loading插件的示例demo
2019/07/03 jQuery
JS实现鼠标移动拖尾
2020/12/27 Javascript
[03:49]DOTA2 2015国际邀请赛中国区预选赛第二日现场百态
2015/05/27 DOTA
Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
2014/06/10 Python
python命令行工具Click快速掌握
2019/07/04 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
TensorFlow2.X结合OpenCV 实现手势识别功能
2020/04/08 Python
在 Python 中使用 7zip 备份文件的操作
2020/12/11 Python
C/C++ 笔试、面试题目大汇总
2015/11/21 面试题
财务与信息服务专业推荐信
2013/11/28 职场文书
酒店总经理欢迎词
2014/01/15 职场文书
捐献物资倡议书范文
2014/05/19 职场文书
幼儿教师辞职信
2015/02/27 职场文书
幼儿园开学温馨提示
2015/07/15 职场文书
当你焦虑迷茫时,请读读这6句话
2019/07/24 职场文书
Redis中缓存穿透/击穿/雪崩问题和解决方法
2021/12/04 Redis
《艾尔登法环》发布最新「战技」宣传片
2022/04/03 其他游戏