Niraj
Niraj

Reputation: 1251

How to get help of a function of a module in python script

How to get help of a function of a module in python script, i have tried following.

import os 

# Stored all the function in a variable.
os_module= dir(os)

function_module_dict = {}
# trying to use help in script 

for function_name in os_module:
    print function_name
    function_module_dict[function_name] = help(os.function_name)

print   function_module_dict

i am getting follwing error ,

AttributeError: 'module' object has no attribute 'function_name'

Upvotes: 1

Views: 149

Answers (2)

timgeb
timgeb

Reputation: 78700

os.function_name tries to access the non-existent attribute literally named 'function_name' from the object os.

For dynamic attribute lookup, you can use getattr. You can use the following code.

import os
import pydoc

help_dict = {}
for function_name in dir(os):
    help_dict[function_name] = pydoc.render_doc(getattr(os, function_name))

The help function returns None, not the docstring. To get the help text as a string, use pydoc.render_doc.

Upvotes: 3

Matthieu Moy
Matthieu Moy

Reputation: 16527

Your problem appears when you call help(os.function_name). Here, os.function_name really means "the function_name field of os", not "the function named after the value of function_name in os".

You need to use getattr(os, function_name) instead. Then, the variable function_name will be evaluated.

Upvotes: 0

Related Questions