您的位置:首页 > 编程语言 > Python开发

python里使用dir函数查看一个类所有成员函数

2017-08-30 17:19 627 查看
如果一个类是别人编写的,又没有帮助文档,怎么样来查看所有成员函数呢?就可以使用下面的代码:
# 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的成员函数。

Python游戏开发入门

http://edu.csdn.net/course/detail/5690

你也能动手修改C编译器

http://edu.csdn.net/course/detail/5582

纸牌游戏开发

http://edu.csdn.net/course/detail/5538 

五子棋游戏开发

http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通 
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战 
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用 
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python tensorflow
相关文章推荐