Search code examples
pythondocstring

Aceess a methods docstring without instance, Python


In Python, how can I access a docstring on a method without an instance of the class?


Solution

  • You can use __doc__:

    class Test():
        def test_method(self):
            """I'm a docstring"""
            print "test method"
    
    
    print Test.test_method.__doc__  # prints "I'm a docstring"
    

    Or, getdoc() from inspect module:

    inspect.getdoc(object)

    Get the documentation string for an object, cleaned up with cleandoc().

    print inspect.getdoc(Test.test_method)  # prints "I'm a docstring"