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实现提取百度搜索结果的方法
May 19 Python
基于asyncio 异步协程框架实现收集B站直播弹幕
Sep 11 Python
python 排序算法总结及实例详解
Sep 28 Python
python 爬虫出现403禁止访问错误详解
Mar 11 Python
Python实现小数转化为百分数的格式化输出方法示例
Sep 20 Python
Python 实现在文件中的每一行添加一个逗号
Apr 29 Python
Python3实现的简单验证码识别功能示例
May 02 Python
python 处理数字,把大于上限的数字置零实现方法
Jan 28 Python
详解Django模版中加载静态文件配置方法
Jul 21 Python
Python中注释(多行注释和单行注释)的用法实例
Aug 28 Python
python中round函数如何使用
Jun 19 Python
python内置进制转换函数的操作
Jun 02 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
phpmyadmin出现Cannot start session without errors问题解决方法
2014/08/14 PHP
完美解决thinkphp验证码出错无法显示的方法
2014/12/09 PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
2015/04/17 PHP
php实现向javascript传递数组的方法
2015/07/27 PHP
php将数组存储为文本文件方法汇总
2015/10/28 PHP
用js实现键盘方向键翻页功能的代码
2007/06/03 Javascript
jQuery(js)获取文字宽度(显示长度)示例代码
2013/12/31 Javascript
wap浏览自动跳转到wap页面的js代码
2014/05/17 Javascript
JQuery悬停控制图片轮播——代码简单
2015/08/05 Javascript
location.hash保存页面状态的技巧
2016/04/28 Javascript
前端jquery部分很精彩
2016/05/03 Javascript
JS制作适用于手机和电脑的通知信息效果
2016/10/28 Javascript
基于Bootstrap和jQuery构建前端分页工具实例代码
2016/11/23 Javascript
JS实现二维数组元素的排列组合运算简单示例
2019/01/28 Javascript
通过实例了解js函数中参数的传递
2019/06/15 Javascript
JS事件流与事件处理程序实例分析
2019/08/16 Javascript
[00:14]护身甲盾
2019/03/06 DOTA
浅谈python中的getattr函数 hasattr函数
2016/06/14 Python
python中函数传参详解
2016/07/03 Python
Python 如何访问外围作用域中的变量
2016/09/11 Python
python利用微信公众号实现报警功能
2018/06/10 Python
django从请求到响应的过程深入讲解
2018/08/01 Python
Pytorch Tensor的索引与切片例子
2019/08/18 Python
python对一个数向上取整的实例方法
2020/06/18 Python
马来西亚银饰品牌:JEOEL
2017/12/15 全球购物
美国地毯购买网站:Rugs USA
2019/02/23 全球购物
俄罗斯药房连锁店:ASNA
2020/06/20 全球购物
报关简历自我评价怎么写
2013/09/19 职场文书
思想品德自我鉴定
2013/10/12 职场文书
无工作经验者个人求职信范文
2013/12/22 职场文书
项目考察欢迎辞
2014/01/17 职场文书
2014年检验科工作总结
2014/11/22 职场文书
2014年办公室人员工作总结
2014/12/09 职场文书
详解Redis瘦身指南
2021/05/26 Redis
Vue vee-validate插件的简单使用
2021/06/22 Vue.js
python全面解析接口返回数据
2022/02/12 Python