2024-04-19 07:59:08 发布
网友
在Python中,如何在没有类实例的情况下访问方法上的docstring?你知道吗
您可以使用__doc__:
__doc__
class Test(): def test_method(self): """I'm a docstring""" print "test method" print Test.test_method.__doc__ # prints "I'm a docstring"
或者,getdoc()来自inspect模块:
inspect
inspect.getdoc(object)Get the documentation string for an object, cleaned up with cleandoc().
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"
您可以在此处使用^{}:
>>> class Test: ... def foo(self, bar): ... """ Returns the parameter passed """ ... return bar ... >>> help(Test.foo)
退货:
Help on method foo in module __main__: foo(self, bar) unbound __main__.Test method Returns the parameter passed (END)
您可以使用
__doc__
:或者,getdoc()来自
inspect
模块:您可以在此处使用^{} :
退货:
相关问题 更多 >
编程相关推荐