1

I am trying to print all the functions and their help docstrings in the strings module but am not getting the desired results. Below are the things which I have tried:

r = 'A random string'

1.  [help(fn) for fn in r.__dir__() if not fn.startswith('__')]
2.  [help(r.fn) for fn in r.__dir__() if not fn.startswith('__')]
3.  [fn.__doc__ for fn in r.__dir__() if not fn.startswith('__')]
4.  [r.fn.__doc__ for fn in r.__dir__() if not fn.startswith('__')]

and a few things more. Some of them throw errors saying that r does not have attribute named 'fn'. Others just print the help documentation for the 'str' function. Is there any way I can print this for all the functions dynamically?

2 Answers 2

1

In python2:

for i in dir(r):
    if not i.startswith('__'):
        print getattr(r, i).__doc__

In python3:

for i in dir(r):
    if not i.startswith('__'):
        print(getattr(r, i).__doc__)

(it's basically the same, changes the print function only). You need to get the method object wth getattr in order to show its __doc__ attribute.

Sign up to request clarification or add additional context in comments.

Comments

0

To print the docstring you use func.__doc__.

r = 'A random string'

for fn in r.__dir__():
  if not fn.startswith("__"):
    print ("Function:",fn)
    print (fn.__doc__)
    print()

1 Comment

Have you even tried running your code. There is an error in your first print statement. It should be print("Function: %s" %fn). Furthermore, this code only prints the documentation of str function multiple times. Please check it once again and revert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.