list of methods for python shell?

后端 未结 9 1370
鱼传尺愫
鱼传尺愫 2021-02-13 21:27

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 obj

相关标签:
9条回答
  • 2021-02-13 21:44
    >>> help(my_object)
    
    0 讨论(0)
  • 2021-02-13 21:46

    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.

    0 讨论(0)
  • 2021-02-13 21:47

    Python supports tab completion as well. I prefer my python prompt clean (so no thanks to IPython), but with tab completion.

    Setup in .bashrc or similar:

    PYTHONSTARTUP=$HOME/.pythonrc
    

    Put this in .pythonrc:

    try:
        import readline
    except ImportError:
        print ("Module readline not available.")
    else:
        print ("Enabling tab completion")
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    

    It will print "Enabling tab completion" each time the python prompt starts up, because it's better to be explicit. This won't interfere with execution of python scripts and programs.


    Example:

    >>> lst = []
    >>> lst.
    lst.__add__(           lst.__iadd__(          lst.__setattr__(
    lst.__class__(         lst.__imul__(          lst.__setitem__(
    lst.__contains__(      lst.__init__(          lst.__setslice__(
    lst.__delattr__(       lst.__iter__(          lst.__sizeof__(
    lst.__delitem__(       lst.__le__(            lst.__str__(
    lst.__delslice__(      lst.__len__(           lst.__subclasshook__(
    lst.__doc__            lst.__lt__(            lst.append(
    lst.__eq__(            lst.__mul__(           lst.count(
    lst.__format__(        lst.__ne__(            lst.extend(
    lst.__ge__(            lst.__new__(           lst.index(
    lst.__getattribute__(  lst.__reduce__(        lst.insert(
    lst.__getitem__(       lst.__reduce_ex__(     lst.pop(
    lst.__getslice__(      lst.__repr__(          lst.remove(
    lst.__gt__(            lst.__reversed__(      lst.reverse(
    lst.__hash__           lst.__rmul__(          lst.sort(
    
    0 讨论(0)
  • 2021-02-13 21:49

    Do this:

    dir(obj)
    
    0 讨论(0)
  • 2021-02-13 21:49

    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.

    0 讨论(0)
  • 2021-02-13 21:49

    Its simple do this for any object you have created

    dir(object)
    

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

    0 讨论(0)
提交回复
热议问题