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获取服务器信息的最简单实现方法
Mar 05 Python
编写同时兼容Python2.x与Python3.x版本的代码的几个示例
Mar 30 Python
python队列通信:rabbitMQ的使用(实例讲解)
Dec 22 Python
python使用Matplotlib绘制分段函数
Sep 25 Python
python 实现将txt文件多行合并为一行并将中间的空格去掉方法
Dec 20 Python
Django框架用户注销功能实现方法分析
May 28 Python
我就是这样学习Python中的列表
Jun 02 Python
Python中的几种矩阵乘法(小结)
Jul 10 Python
Pytorch 中retain_graph的用法详解
Jan 07 Python
使用Python实现批量ping操作方法
May 06 Python
基于PyTorch中view的用法说明
Mar 03 Python
python opencv通过按键采集图片源码
May 20 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 数组教程 定义数组
2009/10/23 PHP
php 定界符格式引起的错误
2011/05/24 PHP
PHP 第三节 变量介绍
2012/04/28 PHP
thinkphp命名空间用法实例详解
2015/12/30 PHP
php语言的7种基本的排序方法
2020/12/28 PHP
PHP创建多级目录的两种方法
2016/10/28 PHP
JavaScript窗口功能指南之在窗口中书写内容
2006/07/21 Javascript
在浏览器中获取当前执行的脚本文件名的代码
2011/07/19 Javascript
JS实现页面超时后自动跳转到登陆页面
2015/01/19 Javascript
原生JS和JQuery动态添加、删除表格行的方法
2015/05/28 Javascript
使用JavaScript制作一个简单的计数器的方法
2015/07/07 Javascript
Javascript BOM学习小结(六)
2015/11/26 Javascript
Vue.js学习笔记之 helloworld
2016/08/14 Javascript
canvas实现绘制吃豆鱼效果
2017/01/12 Javascript
基于vue2.0+vuex的日期选择组件功能实现
2017/03/13 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
JavaScript 实现自己的安卓手机自动化工具脚本(推荐)
2020/05/13 Javascript
vue+iview实现分页及查询功能
2020/11/17 Vue.js
[01:45]绝对公平!DOTA2队长征召模式详解
2014/04/25 DOTA
[02:37]TI8勇士令状不朽珍藏II视频展示
2018/06/23 DOTA
[09:13]DOTA2-DPC中国联赛 正赛 Ehome vs Magma 选手采访 1月19日
2021/03/11 DOTA
tensorflow创建变量以及根据名称查找变量
2018/03/10 Python
使用Python通过win32 COM实现Word文档的写入与保存方法
2018/05/08 Python
python用plt画图时,cmp设置方法
2018/12/13 Python
python定时任务 sched模块用法实例
2019/11/04 Python
浅谈django 重载str 方法
2020/05/19 Python
python利用xlsxwriter模块 操作 Excel
2020/10/14 Python
解决virtualenv -p python3 venv报错的问题
2021/02/05 Python
安踏官方商城:anta.cn
2019/12/16 全球购物
大学四年规划书范文
2013/12/27 职场文书
工艺员岗位职责
2014/02/11 职场文书
新闻编辑自荐书范文
2014/02/12 职场文书
本科毕业生求职自荐信
2014/04/09 职场文书
明星邀请函
2015/02/02 职场文书
超级礼物观后感
2015/06/15 职场文书
解决tk mapper 通用mapper的bug问题
2021/06/16 Java/Android