How to list the synopsis of classes?

Asked

Viewed 88 times

7

I started to venture into python and am making program that lists class properties. I use obj well.dic, help(obj), and dir(obj). Someone knows if there is a command that lists the synopses of the methods, something like this:

For example for a Gtk.Box, someone how to get a result similar to this?

  pack_start(child, expand=True, fill=True, padding=0)
  pack_end(child, expand=True, fill=True, padding=0)
  pack_start_defaults(widget)
  pack_end_defaults(widget)
  pack_end_defaults(widget)
  • is not answer to what questions but, for example after import math, help(math.sin) or on the command line pydoc math.sin can help...

  • All right, I can do that. You wouldn’t happen to know how to get help to pass the text to a string variable, instead of imprinting it? So you could easily locate the part that matters with String.find(text_de_interest).

  • I’m not sure :( -- usually what you want is in math.f.__doc__ for all relevant symbols f in dir(math)

1 answer

2

As it is in the comments - the text that is displayed when the help Python is what is defined in modules, classes or functions as "docstring" - it becomes available as the attribute __doc__ of the object in question, and is created automatically if the first expression within the body of a module, class or function (including method) is a string:

In [6]: import math

In [7]: math.sin.__doc__
Out[7]: 'sin(x)\n\nReturn the sine of x (measured in radians).'

However, objects do not always include a meaningful docstring - and it is not always enough. Python’s help for classes, for example, joins all the method statements, and the docstrings of each, in addition to the class docstring.

Internally, help uses the module pydoc, and, inside, the function that recovers the full text of the help is called render_doc.

Therefore:

>>> import gtk
>>> import pydoc
>>> text = pydoc.render_doc(gtk.Box)
>>> print (text)
...

However - even enderdoc does not retrieve the signature of the functions that are written in C, or if they use the *args, **kwargs feature. This is the case for most Pygtk functions - so it is not possible to have an output like the one you ask for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.