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 合并文件的具体实例
Aug 08 Python
Python 爬虫模拟登陆知乎
Sep 23 Python
Python 模拟员工信息数据库操作的实例
Oct 23 Python
Python 判断是否为质数或素数的实例
Oct 30 Python
python3基于TCP实现CS架构文件传输
Jul 28 Python
Python学习笔记之Zip和Enumerate用法实例分析
Aug 14 Python
python实现递归查找某个路径下所有文件中的中文字符
Aug 31 Python
python使用协程实现并发操作的方法详解
Dec 27 Python
Python tkinter常用操作代码实例
Jan 03 Python
Python3 hashlib密码散列算法原理详解
Mar 30 Python
linux 下selenium chrome使用详解
Apr 02 Python
PyTorch 中的傅里叶卷积实现示例
Dec 11 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制作中间带自己定义图片二维码的方法
2014/01/27 PHP
PHP输出缓存ob系列函数详解
2014/03/11 PHP
CodeIgniter框架过滤HTML危险代码
2014/06/12 PHP
ThinkPHP实现将SESSION存入MYSQL的方法
2014/07/22 PHP
JavaScript 快捷键设置实现代码
2009/03/13 Javascript
JavaScript中获取元素索引的函数
2010/09/10 Javascript
javascript 运算数的求值顺序
2011/08/23 Javascript
jquery获取一组checkbox的值(实例代码)
2013/11/04 Javascript
jquery实现的鼠标拖动排序Li或Table
2014/05/04 Javascript
完美实现八种js焦点轮播图(上篇)
2016/07/18 Javascript
AngularJS入门之动画
2016/07/27 Javascript
easyui tree带checkbox实现单选的简单实例
2016/11/07 Javascript
详解在 Angular 项目中添加 clean-blog 模板
2017/07/04 Javascript
基于jQuery实现无缝轮播与左右点击效果
2018/05/13 jQuery
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
javascript前端实现多视频上传
2020/12/13 Javascript
Python实现统计单词出现的个数
2015/05/28 Python
通过数据库向Django模型添加字段的示例
2015/07/21 Python
深入浅出分析Python装饰器用法
2017/07/28 Python
对python Tkinter Text的用法详解
2018/10/11 Python
Python+selenium点击网页上指定坐标的实例
2019/07/05 Python
Python3和PyCharm安装与环境配置【图文教程】
2020/02/14 Python
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2020/04/27 Python
python将数据插入数据库的代码分享
2020/08/16 Python
CSS3制作气泡对话框的实例教程
2016/05/10 HTML / CSS
详解CSS透明opacity和IE各版本透明度滤镜filter的最准确用法
2016/12/20 HTML / CSS
HTML5 canvas实现移动端上传头像拖拽裁剪效果
2016/03/14 HTML / CSS
德国柯吉澳趣味家居:Koziol
2017/08/24 全球购物
New Balance天猫官方旗舰店:始于1906年,百年慢跑品牌
2017/11/15 全球购物
台湾屈臣氏网路商店:Watsons台湾
2020/12/29 全球购物
一月红领巾广播稿
2014/02/11 职场文书
博士生导师推荐信
2014/07/08 职场文书
名人演讲稿范文
2014/09/16 职场文书
红旗渠导游词
2015/02/09 职场文书
2015年国庆晚会主持词
2015/07/01 职场文书
干部作风纪律整顿心得体会
2016/01/23 职场文书