5

I want to grab the docstring in my commandline application, but every time I call the builtin help() function, Python goes into interactive mode.

How do I get the docstring of an object and not have Python grab focus?

4

3 に答える 3

7

.__doc__次のプロパティを介して任意の docstring を使用できます。

>>> print str.__doc__

Python 3 では、印刷に括弧が必要になります。

>>> print(str.__doc__)
于 2009-08-13T07:50:59.473 に答える
3

dir({insert class name here}を使用)して、クラスの内容を取得し、それを繰り返し処理して、メソッドやその他のものを探すことができます。Taskこの例では、名前で始まるメソッドをクラスで検索し、cmdそのドキュメント文字列を取得します。

command_help = dict()

for key in dir( Task ):
    if key.startswith( 'cmd' ):
        command_help[ key ] = getattr( Task, key ).__doc__
于 2009-08-13T07:54:18.810 に答える