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?
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
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).
say its name is "theobject": dir( theobject )
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.
精彩评论