Pythonのhelp()関数は、指定したモジュール、クラス、関数、変数などのドキュメンテーションを取得するために使用されます。このメソッドは通常、PythonのインタプリタコンソールでPythonオブジェクトの詳細を取得するために使用されます。
Pythonのhelp()関数
Pythonのhelp()関数の構文は次のとおりです:
help([object])
引数が指定されていない場合、対話型ヘルプシステムがインタプリタコンソールで開始されます。Pythonのヘルプコンソールでは、ヘルプドキュメンテーションを取得するためにモジュール、クラス、関数の名前を指定することができます。いくつかの例を挙げると:
help> True
help> collections
help> builtins
help> modules
help> keywords
help> symbols
help> topics
help> LOOPING
ヘルプコンソールから抜けるには、quit
と入力します。また、pythonコンソールから直接help()関数にパラメータを渡してヘルプドキュメンテーションを取得することもできます。
>>> help('collections')
>>> help(print)
>>> help(globals)
では、globals()関数のhelp()関数の出力を見てみましょう。
>>> help('builtins.globals')
Help on built-in function globals in builtins:
builtins.globals = globals()
Return the dictionary containing the current scope's global variables.
NOTE: Updates to this dictionary *will* affect name lookups in the current global scope and vice-versa.
カスタムクラスと関数のためにhelp()を定義する
カスタムクラスと関数のために、docstring(ドキュメンテーション文字列)を定義することで、help()関数の出力を定義することができます。デフォルトでは、メソッドの本体内の最初のコメント文字列がそのドキュメント文字列として使用されます。これは3つのダブルクォートで囲まれています。以下のコードが含まれたpythonファイルpython_help_examples.py
があるとしましょう。
def add(x, y):
"""
This function adds the given integer arguments
:param x: integer
:param y: integer
:return: integer
"""
return x + y
class Employee:
"""
Employee class, mapped to "employee" table in Database
"""
id = 0
name = ''
def __init__(self, i, n):
"""
Employee object constructor
:param i: integer, must be positive
:param n: string
"""
self.id = i
self.name = n
関数、クラス、およびそのメソッドのためにドキュメント文字列を定義していることに注意してください。ドキュメントには一定のフォーマットを従う必要があります。一部はPyCharm IDEを使用して自動的に生成されています。NumPyドキュメント文字列ガイドは、ヘルプのドキュメンテーションの適切な方法についてのアイデアを得るための良い場所です。では、このドキュメント文字列をPythonコンソールでヘルプドキュメントとして取得する方法を見てみましょう。まず、関数とクラスの定義をロードするためにこのスクリプトをコンソールで実行する必要があります。これはexec()
コマンドを使用して行うことができます。
>>> exec(open("python_help_examples.py").read())
globals()コマンドを使用して、関数とクラスの定義が存在することを確認できます。
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__warningregistry__': {'version': 0}, 'add': <function add at 0x100dda1e0>, 'Employee': <class '__main__.Employee'>}
‘Employee’と’add’がグローバルスコープ辞書に存在していることに注意してください。これで、help()関数を使用してヘルプドキュメントを取得することができます。いくつかの例を見てみましょう。
>>> help('python_help_examples')
>>> help('python_help_examples.add')
Help on function add in python_help_examples:
python_help_examples.add = add(x, y)
This function adds the given integer arguments
:param x: integer
:param y: integer
:return: integer
(END)
>>> help('python_help_examples.Employee')
>>> help('python_help_examples.Employee.__init__')
Help on function __init__ in python_help_examples.Employee:
python_help_examples.Employee.__init__ = __init__(self, i, n)
Employee object constructor
:param i: integer, must be positive
:param n: string
(END)
概要
Pythonのhelp()関数は、モジュール、クラス、関数の詳細を取得するのに非常に役立ちます。カスタムのクラスや関数には、使用方法を説明するためのドックストリングを定義するのが常に最善の方法です。
Pythonのスクリプト全体やPythonの例など、詳細は私たちのGitHubリポジトリをご覧ください。
参考: 公式ドキュメント
Source:
https://www.digitalocean.com/community/tutorials/python-help-function