python中查看对象的属性和方法以及如何使用

python中查看对象的属性和方法以及如何使用

.dir()查看对象有什么,help()查看怎么用


Python有众多内置函数,记不住有什么,记不住功能.It’s easy to solve

dir() 带参数时,返回参数的属性、方法列表
help()函数查看函数或模块用途的详细说明

因此先查看有什么再查看如何用

例子

D = [A,B,C]
#print(D)
dir(D)#等价与dir([ ])
help(D.pop)

dir(D)或者dir([ ])将返回如下列表

[‘add’,
class’,
contains’,
delattr’,
delitem’,
dir’,
doc’,
eq’,
format’,
ge’,
getattribute’,
getitem’,
gt’,
hash’,
iadd’,
imul’,
init’,
init_subclass’,
iter’,
le’,
len’,
lt’,
mul’,
ne’,
new’,
reduce’,
reduce_ex’,
repr’,
reversed’,
rmul’,
setattr’,
setitem’,
sizeof’,
str’,
subclasshook’,
‘append’,
‘clear’,
‘copy’,
‘count’,
‘extend’,
‘index’,
‘insert’,
‘pop’,
‘remove’,
‘reverse’,
‘sort’]

其中有列表方法pop,使用help(D.pop)得到使用帮助

Help on built-in function pop:
pop(index=-1, /) method of builtins.list instance
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.

发布了8 篇原创文章 · 获赞 0 · 访问量 21

猜你喜欢

转载自blog.csdn.net/weixin_44827144/article/details/105588338
今日推荐