I want to print the docstring of a python function from inside the function itself. for eg.
def my_function(self):
\"\"\"Doc string for my function.\"\"\"
There's quite a simple method for doing this that nobody has mentioned yet:
import inspect
def func():
"""Doc string"""
print inspect.getdoc(func)
And this does what you want.
There's nothing fancy going on here. All that's happening is that by doing func.__doc__ in a function defers attribute resolution long enough to have looking up __doc__ on it work as you'd expect.
I use this with docopt for console script entry points.