Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting doc string of a python file

Is there a way of getting the doc string of a python file if I have only the name of the file ? For instance I have a python file named a.py. I know that it has a doc string ( being mandated before) but don't know of its internal structure i.e if it has any classes or a main etc ? I hope I not forgetting something pretty obvious If I know it has a main function I can do it this way that is using import

     filename = 'a.py'
     foo = __import__(filename)
     filedescription = inspect.getdoc(foo.main())

I can't just do it this way:

     filename.__doc__    #it does not work
like image 258
johnny alpaca Avatar asked Oct 21 '11 10:10

johnny alpaca


People also ask

How do you access a doc string in Python?

Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function. An object's docstring is defined by including a string constant as the first statement in the object's definition.

How do I access doc string?

All functions should have a docstring. Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function.

What does __ doc __ mean in Python?

The __doc__ attribute Each Python object (functions, classes, variables,...) provides (if programmer has filled it) a short documentation which describes its features. You can access it with commands like print myobject.

How do you print the docstring documentation string of the input function?

just use print(input. doc)


1 Answers

You should be doing...

foo = __import__('a')
mydocstring = foo.__doc__

or yet simpler...

import a
mydocstring = a.__doc__
like image 198
Mike Pennington Avatar answered Oct 11 '22 14:10

Mike Pennington