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.