python利用dir函数查看类中所有成员函数示例代码

yipeiwu_com6年前Python基础

前言

如果一个类是别人编写的,又没有帮助文档,怎么样来查看所有成员函数呢?本文详细给大家介绍了关于python用dir函数查看类中所有成员函数的相关内容,下面话不多说了,来一起看看详细的介绍吧。

可以使用下面的代码:

# File: builtin-dir-example-2.py 
 
class A: 
 def a(self): 
  pass 
 def b(self): 
  pass 
 
class B(A): 
 def c(self): 
  pass 
 def d(self): 
  pass 
 
def getmembers(klass, members=None): 
 # get a list of all class members, ordered by class 
 if members is None: 
  members = [] 
 for k in klass.__bases__: 
  getmembers(k, members) 
 for m in dir(klass): 
  if m not in members: 
   members.append(m) 
 return members 
 
print('A=> :', getmembers(A)) 
print() 
print('B=> :', getmembers(B)) 
print() 
print('IOError=> :', getmembers(IOError)) 

输出结果如下:

>>> 
==== RESTART: D:/work/csdn/python_Game1/example/builtin-dir-example-2.py ====
A=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__dict__', '__module__', '__weakref__', 'a', 'b']


B=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__dict__', '__module__', '__weakref__', 'a', 'b', 'c', 'd']


IOError=> : ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__cause__', '__context__', '__dict__', '__setstate__', '__suppress_context__', '__traceback__', 'args', 'with_traceback', 'characters_written', 'errno', 'filename', 'filename2', 'strerror', 'winerror']
>>> 

在这个例子里,输出基类A的成员函数,输出派生类B的成员函数。

dir()内置函数作用

python内置方法有很多,无论是初学还是经通python的程序员都不能全部记住所有方法,这时候dir()函数就非常有用了。使用dir()函数可以查看对像内所有属于及方法,在python中任何东西都是对像,一种数据类型,一个模块等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。

dir()函数使用方法

dir()函数操作方法很简单,只需要把你想要查询和对像添写到( )括号中就可以使用了。

例如你想查看列表都有哪些方法,你可以在( )中直接传入空列表对像[ ]或是一个列表数据类型的变量名,像下边这样操作:

>>>dir([ ])


x = ['a','b']
>>>dir(x)


两种操作方法所得结果一样,都是查看列表都有哪些操作方法及属性的。如果你想查字符串,只要把()中的参数变量名或空字符串' '就可以了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python-ElasticSearch搜索查询的讲解

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有...

Python学习笔记之Break和Continue用法分析

本文实例讲述了Python学习笔记之Break和Continue用法。分享给大家供大家参考,具体如下: Python 中的Break 和 Continue break:控制何时循环...

对python字典元素的添加与修改方法详解

1、字典中的键存在时,可以通过字典名+下标的方式访问字典中改键对应的值,若键不存在则会抛出异常。如果想直接向字典中添加元素可以直接用字典名+下标+值的方式添加字典元素,只写键想后期对键赋...

Python代码生成视频的缩略图的实例讲解

Reddit 上目前充斥着各种机器人账号,官方也非常支持这种行为,只要不是无意义的发言,机器人多了还能增加活跃度,吸引真人用户一起来各抒己见,比如说每周都有的一个“烦人的星期二”的帖子,...

python绘制雪景图

python绘制雪景图

本文实例为大家分享了python绘制雪景图的具体代码,供大家参考,具体内容如下 绘制雪景图,应用到turtle和random。 from turtle import * from...