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第三方库xlrd/xlwt的安装与读写Excel表格
Jan 21 Python
Python+selenium实现截图图片并保存截取的图片
Jan 05 Python
解决安装tensorflow遇到无法卸载numpy 1.8.0rc1的问题
Jun 13 Python
numpy.std() 计算矩阵标准差的方法
Jul 11 Python
PyQt5实现简易计算器
May 30 Python
Pandas之Dropna滤除缺失数据的实现方法
Jun 25 Python
pytorch 实现模型不同层设置不同的学习率方式
Jan 06 Python
django admin 根据choice字段选择的不同来显示不同的页面方式
May 13 Python
浅谈keras使用中val_acc和acc值不同步的思考
Jun 18 Python
Django REST 异常处理详解
Jul 15 Python
如何通过python检查文件是否被占用
Dec 18 Python
Python之qq自动发消息的示例代码
Feb 18 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实现微信发红包程序
2015/08/24 PHP
PHP微信红包API接口
2015/12/05 PHP
Zend Framework基本页面布局分析
2016/03/19 PHP
Swoole 5将移除自动添加Event::wait()特性详解
2019/07/10 PHP
Jquery ui css framework
2010/06/28 Javascript
javascript 伪数组实现方法
2010/10/11 Javascript
jQuery动态显示和隐藏datagrid中的某一列的方法
2013/12/11 Javascript
jQuery函数的第二个参数获取指定上下文中的DOM元素
2014/05/19 Javascript
如何用jQuery实现ASP.NET GridView折叠伸展效果
2015/09/26 Javascript
jQuery UI插件实现百度提词器效果
2016/11/21 Javascript
js实现简单的获取验证码按钮效果
2017/03/03 Javascript
对vue.js中this.$emit的深入理解
2018/02/23 Javascript
webpack4 CSS Tree Shaking的使用
2018/09/03 Javascript
浅谈React Native 传参的几种方式(小结)
2019/05/21 Javascript
Node对CommonJS的模块规范
2019/11/06 Javascript
原生js+canvas实现贪吃蛇效果
2020/08/02 Javascript
vue使用canvas实现移动端手写签名
2020/09/22 Javascript
Python统计列表中的重复项出现的次数的方法
2014/08/18 Python
机器学习python实战之决策树
2017/11/01 Python
神经网络理论基础及Python实现详解
2017/12/15 Python
Python小游戏之300行代码实现俄罗斯方块
2019/01/04 Python
django实现类似触发器的功能
2019/11/15 Python
pycharm专业版远程登录服务器的详细教程
2020/09/15 Python
Python浮点型(float)运算结果不正确的解决方案
2020/09/22 Python
python绘图模块之利用turtle画图
2021/02/12 Python
html5的画布canvas——画出弧线、旋转的图形实例代码+效果图
2013/06/09 HTML / CSS
HTML最新标准HTML5总结(必看)
2016/06/13 HTML / CSS
波兰办公用品和学校用品在线商店:Dlabiura24.pl
2020/11/18 全球购物
高效课堂标语
2014/06/26 职场文书
机关领导查摆四风思想汇报
2014/09/13 职场文书
放假通知
2015/04/14 职场文书
英语导游欢迎词
2015/09/30 职场文书
2016大学生国家助学贷款承诺书
2016/03/25 职场文书
SQL Server 数据库实验课第五周——常用查询条件
2021/04/05 SQL Server
浅谈 JavaScript 沙箱Sandbox
2021/11/02 Javascript
MySQL中rank() over、dense_rank() over、row_number() over用法介绍
2022/03/23 MySQL