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."""
# print the Docstring here.
At the moment I am doing this directly after my_function has been defined.
print my_function.__doc__
But would rather let the function do this itself.
I have tried calling print self.__doc__ print self.my_function.__doc__ and print this.__doc__ inside my_function but this did not work.
This will work as long as you don’t change the object bound to the name
my_func.Situations in which you’d do this are rather rare, but they do happen.
However, if you write a decorator like this:
Now you can do this:
And this will ensure that your function gets a reference to itself (similar to
self) as its first argument, so it can always get the docstring of the right function. If used on a method, the usualselfbecomes the second argument.