See Class Methods in the Python Console.

If I am dealing with an object in the Python console, is there a way to see which methods are available for this class?

+3
source share
4 answers

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

a = list()
print dir(a)

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

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

Note that in this case both will print the same results, but python is pretty dynamic, you can imagine how to add new methods to the instance without reflecting it in the class.

If you are learning python and want to take advantage of the possibilities of reflecting it in a pleasant environment, I advise you to take a look at ipython. Inside ipython, you get tab completion by methods / attributes

+8

, docstrings , - 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)
 | 

(... ..).

+1

, - "theobject": dir (theobject)

0

If you want to perform a tab, use Ipython or stdlib 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.
0
source

Source: https://habr.com/ru/post/1789235/


All Articles