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中处理列表之reverse()方法的使用教程
May 21 Python
Python内置函数OCT详解
Nov 09 Python
学习python中matplotlib绘图设置坐标轴刻度、文本
Feb 07 Python
python修改txt文件中的某一项方法
Dec 29 Python
python实现图片九宫格分割
Mar 07 Python
python3发送邮件需要经过代理服务器的示例代码
Jul 25 Python
Python 共享变量加锁、释放详解
Aug 28 Python
python用TensorFlow做图像识别的实现
Apr 21 Python
Django自带的用户验证系统实现
Dec 18 Python
python sleep和wait对比总结
Feb 03 Python
从Pytorch模型pth文件中读取参数成numpy矩阵的操作
Mar 04 Python
python index() 与 rindex() 方法的使用示例详解
Dec 24 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
S900/ ETON E1-XM 收音机
2021/03/02 无线电
PHP_Cooikes不同页面无法传递的解决方法
2014/03/07 PHP
destoon安全设置中需要设置可写权限的目录及文件
2014/06/21 PHP
php画图实例
2014/11/05 PHP
PHP学习记录之常用的魔术常量详解
2019/12/12 PHP
基于JQuery的浮动DIV显示提示信息并自动隐藏
2011/02/11 Javascript
Knockout数组(observable)使用详解示例
2013/11/15 Javascript
jQuery实现跨域iframe接口方法调用
2015/03/14 Javascript
js实现简单的验证码
2015/12/25 Javascript
jQuery使用中可能被XSS攻击的一些危险环节提醒
2016/05/24 Javascript
js判断是否为空和typeof的用法(详解)
2016/10/07 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
2016/10/09 Javascript
浅谈js script标签中的预解析
2016/12/30 Javascript
JS验证字符串功能
2017/02/22 Javascript
详解vue axios中文文档
2017/09/12 Javascript
详解angularjs实现echart图表效果最简洁教程
2017/11/29 Javascript
菊花转动的jquery加载动画效果
2018/08/19 jQuery
微信小程序如何实现在线客服功能
2019/10/16 Javascript
element中Steps步骤条和Tabs标签页关联的解决
2020/12/08 Javascript
[01:55]2014DOTA2国际邀请赛 BBC正赛第一天总结
2014/07/10 DOTA
[00:14]PWL:老朋友Mushi拍VLOG与中国玩家问好
2020/11/04 DOTA
利用Python的装饰器解决Bottle框架中用户验证问题
2015/04/24 Python
简单实现python收发邮件功能
2018/01/05 Python
Numpy掩码式数组详解
2018/04/17 Python
Python将文本去空格并保存到txt文件中的实例
2018/07/24 Python
ubuntu16.04制作vim和python3的开发环境
2018/09/23 Python
Python设计模式之外观模式实例详解
2019/01/17 Python
解决python线程卡死的问题
2019/02/18 Python
python paramiko远程服务器终端操作过程解析
2019/12/14 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
2020/06/06 Python
分解成质因数(如435234=251*17*17*3*2,据说是华为笔试题)
2014/07/16 面试题
竞选班干部演讲稿600字
2014/08/20 职场文书
班主任高考寄语
2015/02/26 职场文书
销售内勤岗位职责范本
2015/04/13 职场文书
spring boot中nativeQuery的用法
2021/07/26 Java/Android
在vue中import()语法不能传入变量的问题及解决
2022/04/01 Vue.js