Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check the methods that an object has, in Python?

For example, a list.

l1 = [1, 5 , 7] How do I check the methods that it has?

(l1.append, for example)

Or a string... string.lower(

like image 984
TIMEX Avatar asked Dec 13 '09 22:12

TIMEX


2 Answers

You can use dir to get a list the methods of any object. This is very useful in the interactive prompt:

>>> dir(l1)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__',
'__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__',
'__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

The interesting methods are usually those not starting with underscores. You can write your own version of dir that ignores names starting with underscores if you wish:

>>> mydir = lambda a:[x for x in dir(a) if not x.startswith('_')]
>>> mydir([])
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
like image 106
Mark Byers Avatar answered Nov 15 '22 08:11

Mark Byers


You might want to look at the getmembers function in the inspect module

In [1]: import inspect

In [2]: inspect?
Type:       module
Base Class: <type 'module'>
String Form:    <module 'inspect' from '/usr/lib/python2.6/inspect.pyc'>
Namespace:  Interactive
File:       /usr/lib/python2.6/inspect.py
Docstring:
    Get useful information from live Python objects.

    This module encapsulates the interface provided by the internal special
    attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion.
    It also provides some help for examining source code and class layout.

    Here are some of the useful functions provided by this module:

        ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
            isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
            isroutine() - check object types
        getmembers() - get members of an object that satisfy a given condition

        getfile(), getsourcefile(), getsource() - find an object's source code
        getdoc(), getcomments() - get documentation on an object
        getmodule() - determine the module that an object came from
        getclasstree() - arrange classes so as to represent their hierarchy

        getargspec(), getargvalues() - get info about function arguments
        formatargspec(), formatargvalues() - format an argument spec
        getouterframes(), getinnerframes() - get info about frames
        currentframe() - get the current stack frame
        stack(), trace() - get info about frames on the stack or in a traceback

In [3]: l1=[1,5,7]

In [4]: inspect.getmembers(l1)
Out[4]: 
[('__add__', <method-wrapper '__add__' of list object at 0xa38716c>),
 ('__class__', <type 'list'>),
 ('__contains__', <method-wrapper '__contains__' of list object at 0xa38716c>),
 ('__delattr__', <method-wrapper '__delattr__' of list object at 0xa38716c>),
 ('__delitem__', <method-wrapper '__delitem__' of list object at 0xa38716c>),
 ('__delslice__', <method-wrapper '__delslice__' of list object at 0xa38716c>),
 ('__doc__',
  "list() -> new list\nlist(sequence) -> new list initialized from sequence's items"),
 ('__eq__', <method-wrapper '__eq__' of list object at 0xa38716c>),
 ('__format__', <built-in method __format__ of list object at 0xa38716c>),
 ('__ge__', <method-wrapper '__ge__' of list object at 0xa38716c>),
 ('__getattribute__',
  <method-wrapper '__getattribute__' of list object at 0xa38716c>),
 ('__getitem__', <built-in method __getitem__ of list object at 0xa38716c>),
 ('__getslice__', <method-wrapper '__getslice__' of list object at 0xa38716c>),
 ('__gt__', <method-wrapper '__gt__' of list object at 0xa38716c>),
 ('__hash__', None),
 ('__iadd__', <method-wrapper '__iadd__' of list object at 0xa38716c>),
 ('__imul__', <method-wrapper '__imul__' of list object at 0xa38716c>),
 ('__init__', <method-wrapper '__init__' of list object at 0xa38716c>),
 ('__iter__', <method-wrapper '__iter__' of list object at 0xa38716c>),
 ('__le__', <method-wrapper '__le__' of list object at 0xa38716c>),
 ('__len__', <method-wrapper '__len__' of list object at 0xa38716c>),
 ('__lt__', <method-wrapper '__lt__' of list object at 0xa38716c>),
 ('__mul__', <method-wrapper '__mul__' of list object at 0xa38716c>),
 ('__ne__', <method-wrapper '__ne__' of list object at 0xa38716c>),
 ('__new__', <built-in method __new__ of type object at 0x822be40>),
 ('__reduce__', <built-in method __reduce__ of list object at 0xa38716c>),
 ('__reduce_ex__',
  <built-in method __reduce_ex__ of list object at 0xa38716c>),
 ('__repr__', <method-wrapper '__repr__' of list object at 0xa38716c>),
 ('__reversed__', <built-in method __reversed__ of list object at 0xa38716c>),
 ('__rmul__', <method-wrapper '__rmul__' of list object at 0xa38716c>),
 ('__setattr__', <method-wrapper '__setattr__' of list object at 0xa38716c>),
 ('__setitem__', <method-wrapper '__setitem__' of list object at 0xa38716c>),
 ('__setslice__', <method-wrapper '__setslice__' of list object at 0xa38716c>),
 ('__sizeof__', <built-in method __sizeof__ of list object at 0xa38716c>),
 ('__str__', <method-wrapper '__str__' of list object at 0xa38716c>),
 ('__subclasshook__',
  <built-in method __subclasshook__ of type object at 0x822be40>),
 ('append', <built-in method append of list object at 0xa38716c>),
 ('count', <built-in method count of list object at 0xa38716c>),
 ('extend', <built-in method extend of list object at 0xa38716c>),
 ('index', <built-in method index of list object at 0xa38716c>),
 ('insert', <built-in method insert of list object at 0xa38716c>),
 ('pop', <built-in method pop of list object at 0xa38716c>),
 ('remove', <built-in method remove of list object at 0xa38716c>),
 ('reverse', <built-in method reverse of list object at 0xa38716c>),
 ('sort', <built-in method sort of list object at 0xa38716c>)]
like image 25
John La Rooy Avatar answered Nov 15 '22 07:11

John La Rooy