Jason Swett
Jason Swett

Reputation: 45094

See class methods in Python console

If I'm dealing with an object in the Python console, is there a way to see what methods are available for that class?

Upvotes: 5

Views: 6580

Answers (4)

Gregg Lind
Gregg Lind

Reputation: 21218

If you want tab-completion, use Ipython, or the stdlib's rlcompleter

>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> readline. <TAB PRESSED>
readline.__doc__          readline.get_line_buffer(  readline.read_init_file(
readline.__file__         readline.insert_text(      readline.set_completer(
readline.__name__         readline.parse_and_bind(
>>> readline.

Upvotes: 0

bgporter
bgporter

Reputation: 36504

Another approach that will let you look at the docstrings for the object is to use the built-in function help()

>>> i = 1
>>> help(type(i))
Help on class int in module __builtin__:

class int(object)
 |  int(x[, base]) -> integer
 |  
 |  Convert a string or number to an integer, if possible.  A floating point
 |  argument will be truncated towards zero (this does not include a string
 |  representation of a floating point number!)  When converting a string, use
 |  the optional base.  It is an error to supply a base when converting a
 |  non-string.  If base is zero, the proper base is guessed based on the
 |  string content.  If the argument is outside the integer range a
 |  long object will be returned instead.
 |  
 |  Methods defined here:
 |  
 |  __abs__(...)
 |      x.__abs__() <==> abs(x)
 | 

(...etc).

Upvotes: 1

David Cournapeau
David Cournapeau

Reputation: 80700

If by class, you actually meant the instance you have, you can simply use dir:

a = list()
print dir(a)

If you really meant to see the methods of the class of your object:

a = list()
print dir(a.__class__)

Note that in that case, both would print the same results, but python being quite dynamic, you can imagine attaching new methods to an instance, without it being reflected in the class.

If you are learning python and want to benefit from its reflection capabilities in a nice environment, I advise you to take a look at ipython. Inside ipython, you get tab-completion on methods/attributes

Upvotes: 9

Felice Pollano
Felice Pollano

Reputation: 33252

say its name is "theobject": dir( theobject )

Upvotes: 0

Related Questions