文書化された関数からDocstringsを分離する方法はありますか?

StackOverflow https://stackoverflow.com/questions/4732850

  •  12-10-2019
  •  | 
  •  

質問

私は多くの小さな機能を備えたモジュールに取り組んでいますが、その文書はかなり長くなる傾向があります。 DocStringsは、実際のコードを少し見つけるために長いドキュメントを絶えずスクロールする必要があるため、モジュールの作業を刺激します。

文書化された関数からDocstringsを分離する方法はありますか?コードから離れたファイルの最後に、またはさらに良いファイルでDocStringsを指定できるようにしたいと思います。

役に立ちましたか?

解決

関数のdocstringは特別な属性として利用可能です __doc__.

>>> def f(x):
...     "return the square of x"
...     return x * x
>>> f.__doc__
'return the square of x'
>>> help(f)
(help page with appropriate docstring)
>>> f.__doc__ = "Return the argument squared"
>>> help(f)
(help page with new docstring)

とにかく、それはテクニックを示しています。実際には:

def f(x):
    return x * x

f.__doc__ = """
Return the square of the function argument.

Arguments: x - number to square

Return value: x squared

Exceptions: none

Global variables used: none

Side effects: none

Limitations: none
"""

...またはあなたがあなたのdocstringに入れたいものは何でも。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top