LoginSignup
1
1

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 54. Docstringsとは

Posted at

※この記事はUdemyの
現役シリコンバレーエンジニアが教えるPython3入門+応用+アメリカのシリコンバレー流コードスタイル
の講座を受講した上での、自分用の授業ノートです。
講師の酒井潤さんから許可をいただいた上で公開しています。

■関数の説明の記述

◆関数内に記述する
docstrings
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

一旦サンプルの関数を用意した。
このサンプルの説明書きとして、通常であれば説明する行の上にコメントアウトするが、
関数の場合は内側の先頭に書くのがお約束。

◆docstringsを呼び出す
docstrings
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__)
result
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.

.__doc__のメソッドで、その関数のdocstrinsを呼び出せる。

◆docstringsを呼び出す2
docstrings
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

help(example_func)
result
Help on function example_func in module __main__:

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.

helpを使うことでも、docstringsを呼び出すことができる。

1
1
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
1
1