python利用dir函数查看类中所有成员函数示例代码
作者:caimouse 时间:2021-02-17 19:06:23
前言
如果一个类是别人编写的,又没有帮助文档,怎么样来查看所有成员函数呢?本文详细给大家介绍了关于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)
两种操作方法所得结果一样,都是查看列表都有哪些操作方法及属性的。如果你想查字符串,只要把()中的参数变量名或空字符串' '就可以了。
来源:http://blog.csdn.net/caimouse/article/details/77719348
标签:python,dir函数,成员函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PIL对上传到Django的图片进行处理并保存的实例
2023-04-03 12:40:04
PyQt5 closeEvent关闭事件退出提示框原理解析
2022-10-18 05:51:17
![](https://img.aspxhome.com/file/2023/1/92191_0s.png)
FrontPage XP设计教程3——网页的布局
2008-10-11 12:20:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017122538589s.jpg)
TensorFlow设置日志级别的几种方式小结
2023-07-30 07:57:42
![](https://img.aspxhome.com/file/2023/6/108126_0s.jpg)
python实现控制电脑鼠标和键盘,登录QQ的方法示例
2023-11-19 12:10:26
python连接kafka加载数据的项目实践
2021-04-23 07:14:38
![](https://img.aspxhome.com/file/2023/2/89382_0s.png)
python-opencv 将连续图片写成视频格式的方法
2023-08-25 23:00:06
免费手机号码归属地API查询接口和PHP使用实例分享
2023-10-31 08:06:37
Python属性和内建属性实例解析
2023-06-10 18:22:15
![](https://img.aspxhome.com/file/2023/2/72732_0s.png)
python 爬取豆瓣网页的示例
2021-06-25 22:50:00
![](https://img.aspxhome.com/file/2023/5/86255_0s.png)
Python 八个数据清洗实例代码详解
2022-12-08 23:50:36
Python中的数据对象持久化存储模块pickle的使用示例
2023-09-26 01:26:28
ASP强制刷新和判断文件地址是否存在
2007-09-16 17:11:00
优雅管理Go Project生命周期
2023-06-16 01:10:42
![](https://img.aspxhome.com/file/2023/0/87630_0s.jpg)
将各种类型或对象都转变为数组
2009-12-28 13:19:00
初衷和结果
2009-02-23 12:52:00
详解Go语言Slice作为函数参数的使用
2023-07-16 11:30:20
Python 实现劳拉游戏的实例代码(四连环、重力四子棋)
2022-09-16 00:07:30
![](https://img.aspxhome.com/file/2023/2/110412_0s.png)
python re正则匹配网页中图片url地址的方法
2023-06-15 20:44:20
![](https://img.aspxhome.com/file/2023/4/95234_0s.jpg)
django框架事务处理小结【ORM 事务及raw sql,customize sql 事务处理】
2022-02-27 23:20:03