Python help() 함수

Python 도움말() 함수는 지정된 모듈, 클래스, 함수, 변수 등의 문서를 얻는 데 사용됩니다. 이 방법은 일반적으로 파이썬 인터프리터 콘솔과 함께 사용되어 파이썬 객체에 대한 세부 정보를 얻습니다.

Python 도움말() 함수

Python 도움말() 함수 구문은 다음과 같습니다:

help([object])

인수가 지정되지 않으면 대화식 도움말 시스템이 인터프리터 콘솔에서 시작됩니다. 파이썬 도움말 콘솔에서는 모듈, 클래스, 함수 이름을 지정하여 도움말 문서를 얻을 수 있습니다. 일부 예시는 다음과 같습니다:

help> True

help> collections

help> builtins

help> modules

help> keywords

help> symbols

help> topics

help> LOOPING

도움말 콘솔에서 나가려면 quit를 입력하십시오. 또한 파이썬 콘솔에서 직접 도움말 문서를 얻을 수도 있습니다. 도움말() 함수에 매개변수를 전달하여

>>> help('collections')

>>> help(print)

>>> help(globals)

globals() 함수에 대한 도움말() 함수의 출력은 무엇인지 살펴보겠습니다.

>>> 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() 정의

사용자 정의 클래스 및 함수에 대한 help() 함수 출력을 정의할 수 있습니다. 이를 위해 docstring (문서화 문자열)을 정의합니다. 기본적으로 메서드 본문의 첫 주석 문자열이 해당 메서드의 docstring으로 사용됩니다. 이것은 세 개의 큰따옴표로 둘러싸입니다. 다음과 같은 코드를 가진 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

함수, 클래스 및 해당 메서드에 대한 docstring을 정의했다는 점을 주목하세요. 문서화를 위한 일정한 형식을 따르셔야 합니다. 일부를 PyCharm IDE를 사용하여 자동으로 생성했습니다. NumPy docstring 가이드는 도움말 문서 작성의 올바른 방법에 대한 아이디어를 얻을 수 있는 좋은 자료입니다. 이 docstring을 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)

요약

파이썬의 help() 함수는 모듈, 클래스, 함수에 대한 세부 정보를 얻는 데 매우 유용합니다. 사용자 정의 클래스와 함수에 대한 도움말 문자열을 정의하는 것이 항상 최상의 실천 방법입니다.

GitHub 저장소에서 완전한 파이썬 스크립트와 더 많은 파이썬 예제를 확인할 수 있습니다. GitHub 저장소를 참조하세요.

참고: 공식 문서

Source:
https://www.digitalocean.com/community/tutorials/python-help-function