1. ホーム 
  2. 備忘録 
  3. Python

Docstrings

Docstringsとは

Python のプログラム中に Docstrings というドキュメントコメントを残すことができる

これを残しておくことで、利用者がその場で処理を文字ベースで確認できたり、後からプログラムのドキュメントを出力することができる

ドキュメントコメントは PyCharm上で Ctrl + Alt + S を押下して設定画面を開き、検索窓に "Docstrings" と打ち込むことで設定箇所を確認できる

Docstrings設定画面の画像

Docstring format の箇所でドキュメントコメントのフォーマットの種類を選ぶことができる

以下にはGoogle式のドキュメントコメントの書き方を記載する


def example_func(param1, param2):
    """Example function with types documented in the docstring.

    Args:
        param1 (int): The first parameter.
        param2 (str): The second parameter.

    Returns:
        bool: The return value. True for success, False otherwise.

    """
    print(param1)
    print(param2)
    return True


print(example_func.__doc__)
help(example_func)
"""
↑ はどちらもドキュメントコメントを表示する
Example function with types documented in the docstring.

    Args:
        param1 (int): The first parameter.
        param2 (str): The second parameter.

    Returns:
        bool: The return value. True for success, False otherwise.
"""