Reputation: 11891
In Python, how can I access a docstring on a method without an instance of the class?
Upvotes: 1
Views: 62
Reputation: 59974
You can use help()
here:
>>> class Test:
... def foo(self, bar):
... """ Returns the parameter passed """
... return bar
...
>>> help(Test.foo)
Returns:
Help on method foo in module __main__:
foo(self, bar) unbound __main__.Test method
Returns the parameter passed
(END)
Upvotes: 1
Reputation: 473843
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"
Upvotes: 3