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使用webbrowser浏览指定url的方法
Apr 04 Python
在Python中处理XML的教程
Apr 29 Python
Python设计模式中单例模式的实现及在Tornado中的应用
Mar 02 Python
python中os和sys模块的区别与常用方法总结
Nov 14 Python
pandas 数据结构之Series的使用方法
Jun 21 Python
django之自定义软删除Model的方法
Aug 14 Python
Python如何使用argparse模块处理命令行参数
Dec 11 Python
在jupyter notebook 添加 conda 环境的操作详解
Apr 10 Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
Apr 27 Python
python中逻辑与或(and、or)和按位与或异或(&、|、^)区别
Aug 05 Python
pytorch显存一直变大的解决方案
Apr 08 Python
Python实现归一化算法详情
Mar 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数组索引的Key加引号和不加引号的区别
2014/08/19 PHP
php画图实例
2014/11/05 PHP
PHP生成腾讯云COS接口需要的请求签名
2018/05/20 PHP
Laravel中9个不经常用的小技巧汇总
2019/04/16 PHP
一个用js实现的页内搜索代码
2007/05/23 Javascript
如何用js控制frame的隐藏或显示的解决办法
2013/03/20 Javascript
关于IE BUG与字符串截取substr的解决办法
2013/04/10 Javascript
jQuery输入城市查看地图使用介绍
2013/05/08 Javascript
用jquery方法操作radio使其默认选项是否
2013/09/10 Javascript
Ajax提交与传统表单提交的区别说明
2014/02/07 Javascript
自己使用js/jquery写的一个定制对话框控件
2014/05/02 Javascript
jQuery的position()方法详解
2015/07/19 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
JS如何判断json是否为空
2016/07/06 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
2016/09/29 Javascript
微信小程序 购物车简单实例
2016/10/24 Javascript
vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)
2019/04/12 Javascript
javascript实现遮罩层动态效果实例
2019/05/14 Javascript
[09:37]DOTA2卡尔工作室 英雄介绍圣堂刺客篇
2013/06/13 DOTA
[50:58]2018DOTA2亚洲邀请赛 4.1 小组赛 B组 Mineski vs EG
2018/04/03 DOTA
Django命名URL和反向解析URL实现解析
2019/08/09 Python
在服务器上安装python3.8.2环境的教程详解
2020/04/26 Python
使用python实现名片管理系统
2020/06/18 Python
Kidsroom台湾:来自德国的婴儿用品
2017/12/11 全球购物
中科软笔试题和面试题
2014/10/07 面试题
电脑教师的自我评价
2013/12/18 职场文书
英语商务邀请函范文
2014/01/16 职场文书
法学函授自我鉴定
2014/02/06 职场文书
《小山羊和小灰兔》教学反思
2014/02/19 职场文书
爱国主义教育基地观后感
2015/06/18 职场文书
小学体育组工作总结
2015/08/13 职场文书
小学教师教学反思
2016/02/24 职场文书
如何使用vue3打造一个物料库
2021/05/08 Vue.js
Python基础数据类型tuple元组的概念与用法
2021/08/02 Python
基于Pygame实现简单的贪吃蛇游戏
2021/12/06 Python
git stash(储藏)的用法总结
2022/06/25 Servers