LoginSignup
0
0

More than 1 year has passed since last update.

[Python]docstringのGoogleスタイルの書き方

Posted at

docstringとは

docstringとは、関数やクラスに対する説明を記載する方法の1つ。docstringを記載することで、ドキュメントをHTML形式で自動生成することもできる。docstringの基本的な書き方としては、関数やクラス定義の先頭に'''または"""で説明を囲む形で記載する。慣例的に'''よりも"""が使用されることが多い。説明文を関数やクラス定義の先頭に記載せず、間に処理が記載されるとdocstringとして認識されなくなる。

def my_function():
    """<関数の説明を記載>"""

またdocstringには、引数や返り値などの書き方に以下のような3つのスタイルがある。
・reStructuredTextスタイル
・NumPyスタイル
・Googleスタイル
その中のGoogleスタイルの書き方について簡単にメモしておく。

Googleスタイル

GoogleのPythonスタイルガイドで定義されているdocstringのスタイル。
Googleスタイルで定義されているセッションの一部を使用した記載例は以下となる。

class Sample():
    """<クラスの概要>

    <クラスの詳細説明>

  Attributes:
        <属性名> (<型>): <説明>
    """

    def my_function():
        """<関数の概要>

        <関数の詳細説明>

        Args:
            <引数名> (<引数の型>): <説明>

        Returns:
            <返り値の型>: <説明>

        Raises:
            <例外名>: <説明>
        """

        <関数の処理内容>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0