How to print Docstring of python function from inside the function itself?

后端 未结 8 1646
無奈伤痛
無奈伤痛 2020-12-04 23:26

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.\"\"\"
         


        
8条回答
  •  心在旅途
    2020-12-05 00:08

    As noted many times, using the function name is a dynamic lookup in the globals() directory. It only works in the module of the definition and only for a global function. If you want to find out the doc string of a member function, you would need to also lookup the path from the class name - which is quite cumbersome as these names can get quite long:

    def foo():
        """ this is foo """
        doc = foo.__doc__
    class Foo:
        def bar(self):
           """ this is bar """
           doc = Foo.bar.__doc__
    

    is equivalent to

    def foo():
        """ this is foo """
        doc = globals()["foo"].__doc__
    class Foo:
        def bar(self):
           """ this is bar """
           doc = globals()["Foo"].bar.__doc__
    

    If you want to look up the doc string of the caller, that won't work anyway as your print-helper might live in a completely different module with a completely different globals() dictionary. The only correct choice is to look into the stack frame - but Python does not give you the function object being executed, it only has a reference to the "f_code" code object. But keep going, as there is also a reference to the "f_globals" of that function. So you can write a function to get the caller's doc like this, and as a variation from it, you get your own doc string.

    import inspect
    
    def get_caller_doc():
        frame = inspect.currentframe().f_back.f_back
        for objref in frame.f_globals.values():
            if inspect.isfunction(objref):
                if objref.func_code == frame.f_code:
                    return objref.__doc__
            elif inspect.isclass(objref):
                for name, member in inspect.getmembers(objref):
                    if inspect.ismethod(member):
                        if member.im_func.func_code == frame.f_code:
                            return member.__doc__
    

    and let's go to test it:

    def print_doc():
       print get_caller_doc()
    
    def foo():
       """ this is foo """
       print_doc()
    
    class Foo:
        def bar(self):
           """ this is bar """
           print_doc()
    
    def nothing():
        print_doc()
    
    class Nothing:
        def nothing(self):
            print_doc()
    
    foo()
    Foo().bar()
    
    nothing()
    Nothing().nothing()
    
    # and my doc
    
    def get_my_doc():
        return get_caller_doc()
    
    def print_my_doc():
        """ showing my doc """
        print get_my_doc()
    
    print_my_doc()
    

    results in this output

     this is foo 
     this is bar 
    None
    None
     showing my doc 
    

    Actually, most people want their own doc string only to hand it down as an argument, but the called helper function can look it up all on its own. I'm using this in my unittest code where this is sometimes handy to fill some logs or to use the doc string as test data. That's the reason why the presented get_caller_doc() only looks for global test functions and member functions of a test class, but I guess that is enough for most people who want to find out about the doc string.

    class FooTest(TestCase):
        def get_caller_doc(self):
            # as seen above
        def test_extra_stuff(self):
            """ testing extra stuff """
            self.createProject("A")
        def createProject(self, name):
            description = self.get_caller_doc()
            self.server.createProject(name, description)
    

    To define a proper get_frame_doc(frame) with sys._getframe(1) is left to the reader().

提交回复
热议问题