51

Is there a function in Python to list the attributes and methods of a particular object?

Something like:

ShowAttributes ( myObject )

   -> .count
   -> .size

ShowMethods ( myObject )

   -> len
   -> parse
n611x007
  • 8,952
  • 8
  • 59
  • 102
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • Duplicate: http://stackoverflow.com/questions/546337/how-do-i-perform-introspection-on-an-object-in-python-2-x – S.Lott Mar 26 '09 at 19:41
  • And this, too: http://stackoverflow.com/questions/192109/is-there-a-function-in-python-to-print-all-the-current-properties-and-values-of-a – S.Lott Mar 26 '09 at 20:39
  • [`property`](https://docs.python.org/2/library/functions.html?highlight=property#property) is a name given to a different concept in python. the term `attribute` would suit you better. an in-depth reading that I like, about both is http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html – n611x007 Jun 18 '15 at 13:57

5 Answers5

68

You want to look at the dir() function:

>>> li = []
>>> dir(li)      
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']

li is a list, so dir(li) returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not the methods themselves.


Edit in response to comment:

No this will show all inherited methods as well. Consider this example:

test.py:

class Foo:
    def foo(): pass

class Bar(Foo):
    def bar(): pass

Python interpreter:

>>> from test import Foo, Bar
>>> dir(Foo)
['__doc__', '__module__', 'foo']
>>> dir(Bar)
['__doc__', '__module__', 'bar', 'foo']

You should note that Python's documentation states:

Note: Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.

Therefore it's not safe to use in your code. Use vars() instead. Vars() doesn't include information about the superclasses, you'd have to collect them yourself.


If you're using dir() to find information in an interactive interpreter, consider the use of help().

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
12

Don't dir() and vars() suit you?

Anonymous
  • 3,011
  • 4
  • 24
  • 23
  • 2
    Everybody must read this http://docs.python.org/library/functions.html right after reading the Python tutorial. – Anonymous Mar 26 '09 at 19:54
11

and for a more human-readable way, you can use see:

In [1]: from see import see
In [2]: x = "hello world!"
In [3]: see(x)
Out[3]: 
  []   in   +   *   %   <   <=   ==   !=   >   >=   hash()   help()   len()
  repr()   str()   .capitalize()   .center()   .count()   .decode()
  .encode()   .endswith()   .expandtabs()   .find()   .format()   .index()
  .isalnum()   .isalpha()   .isdigit()   .islower()   .isspace()   .istitle()
  .isupper()   .join()   .ljust()   .lower()   .lstrip()   .partition()
  .replace()   .rfind()   .rindex()   .rjust()   .rpartition()   .rsplit()
  .rstrip()   .split()   .splitlines()   .startswith()   .strip()
  .swapcase()   .title()   .translate()   .upper()   .zfill()
Autoplectic
  • 7,566
  • 30
  • 30
  • superb! one thing I miss is that you cannot get `help()` or `__doc__` for a thing like `<=`, so you should know what you are doing already, regarding operators. well, if you don't know their magic methods from head. :) altogether a minor issue and a superb package! – n611x007 Jun 18 '15 at 14:01
2

Another way to do this is with the nifty IPython environment. It lets you tab complete to find all the methods and fields of an object.

RossFabricant
  • 12,364
  • 3
  • 41
  • 50
-1

It is surprising to me that no one mentioned the python object function:
keys()

Evhz
  • 8,852
  • 9
  • 51
  • 69
  • 1
    `keys()` is not a builtin function in python. You can call `.keys()` on a dict, but it doesn't meet OP's needs. – cmc Aug 01 '21 at 13:03