Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

python - How to automatically generate docstrings including nested functions?

How to generate automatically docs for the c_nested function?

Background: I do code documentation for other developers and I would like to automatically generate a summary of all class methods included nested functions with short description (docstring).

When I run help(A) on class A I get:

Help on class A in module __main__:

class A(builtins.object)
 |  A(a)
 |  
 |  doc A
 |  
 |  Methods defined here:
 |  
 |  __init__(self, a)
 |      Initialize self.  See 
 |  
 |  b_method(self)
 |      doc b_method

Requested output: c_nested() with docstring: (Docs could be printed event with script, it doesn't need to be printed with pydoc help.)

Help on class A in module __main__:

class A(builtins.object)
 |  A(a)
 |  
 |  doc A
 |  
 |  Methods defined here:
 |  
 |  __init__(self, a)
 |      Initialize self.  See 
 |  
 |  b_method(self)
 |      doc b_method
 |
 |           c_nested()
 |                doc c_nested

Class example:

class A:
    """ doc A """
    def __init__(self,a):
        self.a = a

    def b_method(self):
        """ doc b_method """

        def c_nested():
            """doc  c_nested """
            pass

        return c_nested()
question from:https://stackoverflow.com/questions/65860067/how-to-automatically-generate-docstrings-including-nested-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Local functions are not publicly visible, so their docstring won't be included in the help.

If you want the function to be shown in the help, make it a module-level function or a method of the class.

See also Are docstrings for internal functions (python) necessary?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...