list of methods for python shell?

2019-03-25 05:52发布

You'd have already found out by my usage of terminology that I'm a python n00b.

straight forward question:

How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?

9条回答
Melony?
2楼-- · 2019-03-25 06:28

If you want only methods, then

def methods(obj):
    return [attr for attr in dir(obj) if callable(getattr(obj, attr))]

But do try out IPython, it has tab completion for object attributes, so typing obj.<tab> shows you a list of available attributes on that object.

查看更多
3楼-- · 2019-03-25 06:31

Its simple do this for any object you have created

dir(object)

it will return a list of all the attributes of the object.

查看更多
倾城 Initia
4楼-- · 2019-03-25 06:34

dir( object )

will give you the list.

for instance:

a = 2
dir( a )

will list off all the methods you can call for an integer.

查看更多
登录 后发表回答