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导入txt数据到mysql的方法
Apr 08 Python
在Python中处理字符串之ljust()方法的使用简介
May 19 Python
python 中的divmod数字处理函数浅析
Oct 17 Python
python装饰器简介---这一篇也许就够了(推荐)
Apr 01 Python
Python字符串、列表、元组、字典、集合的补充实例详解
Dec 20 Python
pytorch 实现在预训练模型的 input上增减通道
Jan 06 Python
Python气泡提示与标签的实现
Apr 01 Python
基于python实现ROC曲线绘制广场解析
Jun 28 Python
python 使用elasticsearch 实现翻页的三种方式
Jul 31 Python
虚拟环境及venv和virtualenv的区别说明
Feb 05 Python
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
Feb 16 Python
利用Python多线程实现图片下载器
Mar 25 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分页函数
2006/10/09 PHP
PHP基础知识回顾
2012/08/16 PHP
PHP微信模板消息操作示例
2017/06/29 PHP
PJ Blog修改-禁止复制的代码和方法
2006/10/25 Javascript
onmouseover和onmouseout的一些问题思考
2013/08/14 Javascript
Javascript 赋值机制详解
2014/11/23 Javascript
基于jquery实现页面滚动时顶部导航显示隐藏
2020/04/20 Javascript
js判断移动端是否安装某款app的多种方法
2015/12/18 Javascript
jQuery实现的精美平滑二级下拉菜单效果代码
2016/03/28 Javascript
jQuery Mobile 触摸事件实例
2016/06/04 Javascript
jQuery实现的简单百分比进度条效果示例
2016/08/01 Javascript
canvas实现图像放大镜
2017/02/06 Javascript
将angular-ui的分页组件封装成指令的方法详解
2017/05/10 Javascript
vue 修改 data 数据问题并实时显示的方法
2018/08/27 Javascript
jquery实现的分页显示功能示例
2019/08/23 jQuery
vue 解决mintui弹窗弹起来,底部页面滚动bug问题
2020/11/12 Javascript
vuex页面刷新导致数据丢失的解决方案
2020/12/10 Vue.js
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
PyTorch预训练的实现
2019/09/18 Python
python实现通过队列完成进程间的多任务功能示例
2019/10/28 Python
Python读取excel文件中带公式的值的实现
2020/04/17 Python
Python钉钉报警及Zabbix集成钉钉报警的示例代码
2020/08/17 Python
关于HTML5的22个初级技巧(图文教程)
2012/06/21 HTML / CSS
非凡女性奢华谦虚风格:The Modist
2017/10/28 全球购物
团支书的期末学习总结自我评价
2013/11/01 职场文书
金融专业推荐信
2013/11/14 职场文书
外语专业毕业生个人的自荐信
2013/11/19 职场文书
2014信息技术专业毕业生自我评价
2014/01/17 职场文书
农贸市场管理制度
2014/01/31 职场文书
幼儿园优秀班主任事迹材料
2014/05/14 职场文书
空气环保标语
2014/06/12 职场文书
党的群众路线整改落实情况汇报
2014/10/28 职场文书
导游词300字
2015/02/13 职场文书
圣诞晚会主持词
2015/07/01 职场文书
2016年优秀共产党员先进事迹材料
2016/02/29 职场文书
2016年基层党组织创先争优承诺书
2016/03/25 职场文书