Вопрос

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

Это было полезно?

Решение

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"

Другие советы

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) 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top