Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python introspection: access function name and docstring inside function definition

Tags:

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"
like image 583
D R Avatar asked Apr 06 '12 00:04

D R


2 Answers

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.

like image 153
aaronasterling Avatar answered Sep 27 '22 19:09

aaronasterling


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')
like image 43
D R Avatar answered Sep 27 '22 20:09

D R