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测试驱动开发实例
Oct 08 Python
详解在Python程序中使用Cookie的教程
Apr 30 Python
scrapy爬虫实例分享
Dec 28 Python
pandas系列之DataFrame 行列数据筛选实例
Apr 12 Python
Python实现的特征提取操作示例
Dec 03 Python
python3中rank函数的用法
Nov 27 Python
pycharm快捷键汇总
Feb 14 Python
python 使用cx-freeze打包程序的实现
Mar 14 Python
利用python实现凯撒密码加解密功能
Mar 31 Python
使用Keras加载含有自定义层或函数的模型操作
Jun 10 Python
Python 数据的累加与统计的示例代码
Aug 03 Python
vscode配置anaconda3的方法步骤
Aug 08 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
一个简单的自动发送邮件系统(二)
2006/10/09 PHP
免费手机号码归属地API查询接口和PHP使用实例分享
2014/04/10 PHP
用htc组件制作windows选项卡
2007/01/13 Javascript
JS打开新窗口的2种方式
2013/04/18 Javascript
利用JS来控制键盘的上下左右键(示例代码)
2013/12/14 Javascript
JS关闭窗口与JS关闭页面的几种方法小结
2013/12/17 Javascript
jquery实现可拖拽弹出层特效
2015/01/04 Javascript
jQuery实现鼠标双击Table单元格变成文本框及输入内容后更新到数据库的方法
2015/11/25 Javascript
一篇文章掌握RequireJS常用知识
2016/01/26 Javascript
Vue.JS入门教程之自定义指令
2016/12/08 Javascript
JS实现合并json对象的方法
2017/10/10 Javascript
NodeJS模块与ES6模块系统语法及注意点详解
2019/01/04 NodeJs
基于Node.js的大文件分片上传示例
2019/06/19 Javascript
es6中let和const的使用方法详解
2020/02/24 Javascript
vue模块移动组件的实现示例
2020/05/20 Javascript
vue 解决addRoutes多次添加路由重复的操作
2020/08/04 Javascript
Python自动扫雷实现方法
2015/07/25 Python
详解Python中的变量及其命名和打印
2016/03/11 Python
python二分查找算法的递归实现方法
2016/05/12 Python
python2.7+selenium2实现淘宝滑块自动认证功能
2018/02/24 Python
对numpy中array和asarray的区别详解
2018/04/17 Python
python用户评论标签匹配的解决方法
2018/05/31 Python
Python如何爬取微信公众号文章和评论(基于 Fiddler 抓包分析)
2019/06/28 Python
pytorch 实现cross entropy损失函数计算方式
2020/01/02 Python
Python requests设置代理的方法步骤
2020/02/23 Python
python实现Oracle查询分组的方法示例
2020/04/30 Python
python实现ping命令小程序
2020/12/28 Python
HTML5中的网络存储实现方式
2020/04/28 HTML / CSS
BAILEY 44官网:美国制造的女性服装
2019/07/01 全球购物
EMPHASIS艾斐诗官网:周生生旗下原创精品珠宝品牌
2020/12/17 全球购物
工程项目经理岗位职责
2013/12/15 职场文书
学校元旦晚会方案
2014/02/19 职场文书
人事任命书怎么写
2014/06/05 职场文书
毕业实习计划书
2015/01/16 职场文书
力克胡哲观后感
2015/06/10 职场文书
小学班主任心得体会
2016/01/07 职场文书