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列表去重的二种方法
Feb 14 Python
详解Python中列表和元祖的使用方法
Apr 25 Python
Python排序搜索基本算法之堆排序实例详解
Dec 08 Python
python求解数组中两个字符串的最小距离
Sep 27 Python
Python时间和字符串转换操作实例分析
Mar 16 Python
Django 表单模型选择框如何使用分组
May 16 Python
Python使用百度翻译开发平台实现英文翻译为中文功能示例
Aug 08 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
python如何提取英语pdf内容并翻译
Mar 03 Python
Python3爬虫中Selenium的用法详解
Jul 10 Python
Python实现文件压缩和解压的示例代码
Aug 12 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
Jan 15 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 观察者模式的实现代码
2013/05/10 PHP
thinkphp文件处理类Dir.class.php的用法分析
2014/12/08 PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
2015/07/29 PHP
PHP如何将XML转成数组
2016/04/04 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
2017/01/13 PHP
thinkphp整合系列之极验滑动验证码geetest功能
2019/06/18 PHP
aspx中利用js实现确认删除代码
2010/07/22 Javascript
一些实用的jQuery代码片段收集
2011/07/12 Javascript
详解jquery uploadify 上传文件
2013/11/09 Javascript
js使下拉列表框可编辑不止是选择
2013/12/12 Javascript
jQuery实现右键菜单、遮罩等效果代码
2016/09/27 Javascript
详解JavaScript 中getElementsByName在IE中的注意事项
2017/02/21 Javascript
js获取css的各种样式并且设置他们的方法
2017/08/22 Javascript
简单谈谈javascript高级特性
2019/09/04 Javascript
浅析vue-cli3配置webpack-bundle-analyzer插件【推荐】
2019/10/23 Javascript
JS实现随机点名器
2020/04/12 Javascript
Vue常用API、高级API的相关总结
2021/02/02 Vue.js
python 自动提交和抓取网页
2009/07/13 Python
python 爬取微信文章
2016/01/30 Python
Python的for和break循环结构中使用else语句的技巧
2016/05/24 Python
神经网络python源码分享
2017/12/15 Python
Window环境下Scrapy开发环境搭建
2018/11/18 Python
padas 生成excel 增加sheet表的实例
2018/12/11 Python
详解python实现交叉验证法与留出法
2019/07/11 Python
python生成器用法实例详解
2019/11/22 Python
从0到1使用python开发一个半自动答题小程序的实现
2020/05/12 Python
波比布朗英国官网:Bobbi Brown英国
2017/11/13 全球购物
英国领先的亚洲旅游专家:Wendy Wu Tours
2018/01/21 全球购物
Linux内核的同步机制是什么?主要有哪几种内核锁
2016/07/11 面试题
中文专业学生自我评价范文
2014/02/06 职场文书
《囚绿记》教学反思
2014/03/01 职场文书
《四季》教学反思
2014/04/08 职场文书
交通事故协议书
2014/04/15 职场文书
六查六看自检自查剖析材料
2014/10/14 职场文书
2014年库房工作总结
2014/11/26 职场文书
乐山大佛导游词
2015/02/02 职场文书