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.\"\"\"
def my_func():
"""Docstring goes here."""
print my_func.__doc__
This will work as long as you don't change the object bound to the name my_func.
new_func_name = my_func
my_func = None
new_func_name()
# doesn't print anything because my_func is None and None has no docstring
Situations in which you'd do this are rather rare, but they do happen.
However, if you write a decorator like this:
def passmein(func):
def wrapper(*args, **kwargs):
return func(func, *args, **kwargs)
return wrapper
Now you can do this:
@passmein
def my_func(me):
print me.__doc__
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 usual self becomes the second argument.