python “help” function: printing docstrings

前端 未结 4 1797
小鲜肉
小鲜肉 2020-12-09 17:30

Is there an option to print the output of help(\'myfun\'). The behaviour I\'m seeing is that output is printed to std.out and the script waits for user input (i.e. type \'q\

4条回答
  •  情书的邮戳
    2020-12-09 18:18

    You've already seen reference to the docstring, the magic __doc__ variable which holds the body of the help:

    def foo(a,b,c): 
       ''' DOES NOTHING!!!! '''
       pass
    
    print foo.__doc__ # DOES NOTHING!!!!
    

    To get the name of a function, you just use __name__:

    def foo(a,b,c): pass
    
    print foo.__name__ # foo
    

    The way to get the signature of a function which is not built in you can use the func_code property and from that you can read its co_varnames:

    def foo(a,b,c): pass
    print foo.func_code.co_varnames # ('a', 'b', 'c')
    

    I've not found out how to do the same for built in functions.

提交回复
热议问题