Python: How to store the documentation(help) of a class in a String?

Asked

Viewed 124 times

5

I’m doing a Python class explorer. I can store class methods and attributes with

 dir(obj) 

and place them in a Treeview. I would like to display the references of this when clicked. For this I would use the text of

 help(obj)

of the class in question. For this you would need the text stored in a String, and so locate the references on this one with

 String.find(nome_do_atributo)

The problem is, when I call help(obj), it prints the text in outuput. Does anyone know how to have the help(obj) text stored in a variable, instead of being printed in the system output?

2 answers

1

No matter what, you could use the attribute _doc_ of the function or class

eg:

def foo():
    """Minha docstring epicamente epica"""
    pass


class Bar():
    """Minha documentacao 100% profissional"""
    pass

func_docstring = foo.__doc__
class_docstring = Bar.__doc__

print(func_docstring)
print(class_docstring)

# Minha docstring epicamente epica
# Minha documentacao 100% profissional

0

I’m sorry about my barbering, but I managed to find the solution.

First, you have to import the pydoc

import pydoc

In Python 3:

string_help = pydoc.render_doc(nome_da_classe, renderer=pydoc.plaintext)

And if the class name comes as string:

string_help = pydoc.render_doc(eval("nome_da_classe"), renderer=pydoc.plaintext)

Caution: the Eval has its risks because it performs anything!!!

In Phython 2, I have not tried, but they say it is so, I will pass in good faith, in case it doesn’t work, let me know.

 string_help = pydoc.plain(pydoc.render_doc(nome_da_classe))

I hope it helps.

Browser other questions tagged

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