tags:

views:

483

answers:

4

Is there a method in Python to list the properties and methods of a particular object?

Something like:

ShowProperties ( myObject )

   -> .count
   -> .size

ShowMethods ( myObject )

   -> len
   -> parse
+6  A: 

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

Anonymous
Awesome, I didn't know these.
Joan Venge
Everybody must read this http://docs.python.org/library/functions.html right after reading the Python tutorial.
Anonymous
+13  A: 

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 save 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().

Andrew Hare
Also this just lists the methods directly applicable to this object type, not its parent class or parent's parent class, right?
Joan Venge
No, inherited methods as well - please see my edit.
Andrew Hare
thanks, I learned from this as well.
SD
Thanks again, but does it also print properties of an object?
Joan Venge
No, it prints just methods.
Andrew Hare
+1  A: 

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
+3  A: 

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