Consider the following python code:
def function():
"Docstring"
name = ???
doc = ???
return name, doc
>>> function()
"function", "Docstring"
What do I need to replace the question marks with so that I get the name and the docstring of the function from inside the same function?
EDIT: Most of the answers so far explicitly hardcode the name of the function inside its definition. Is it possible do something like below where a new function get_name_doc would access the function from the outer frame from which it is called, and return its name and doc?
def get_name_doc():
???
def function():
"Docstring"
name, doc = get_name_doc()
return name, doc
>>> function()
"function", "Docstring"
This is not possible to do cleanly in a consistent way because names can be changed and reassigned.
However, you can use this so long as the function isn't renamed or decorated.
>>> def test():
... """test"""
... doc = test.__doc__
... name = test.__name__
... return doc, name
...
>>> test()
('test', 'test')
>>>
It's not at all reliable. Here's an example of it going wrong.
>>> def dec(f):
... def wrap():
... """wrap"""
... return f()
... return wrap
...
>>> @dec
... def test():
... """test"""
... return test.__name__, test.__doc__
...
>>> test()
('wrap', 'wrap')
>>>
This is because the name test
isn't defined at the time that the function is actually created and is a global reference in the function. It hence gets looked up in the global scope on every execution. So changes to the name in the global scope (such as decorators) will break your code.
The code below solves the problem for the name of the function. However, it fails to detect the correct docstring for the example given by aaronasterling. I wonder if there is a way to get back to the abstract syntax tree associated with a bytecode object. Then it would be quite easy to read the docstring.
import inspect
def get_name_doc():
outerframe = inspect.currentframe().f_back
name = outerframe.f_code.co_name
doc = outerframe.f_back.f_globals[name].__doc__
return name, doc
if __name__ == "__main__":
def function():
"Docstring"
name, doc = get_name_doc()
return name, doc
def dec(f):
def wrap():
"""wrap"""
return f()
return wrap
@dec
def test():
"""test"""
return get_name_doc()
assert function() == ('function', "Docstring")
#The assertion below fails:. It gives: ('test', 'wrap')
#assert test() == ('test', 'test')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With