웹사이트 검색

파이썬 도움말() 함수


Python help() 함수는 지정된 모듈, 클래스, 함수, 변수 등의 문서를 가져오는 데 사용됩니다. 이 방법은 일반적으로 Python 인터프리터 콘솔과 함께 사용되어 Python 개체에 대한 세부 정보를 가져옵니다.

파이썬 도움말() 함수

Python help() 함수 구문은 다음과 같습니다.

help([object])
help> True

help> collections

help> builtins

help> modules

help> keywords

help> symbols

help> topics

help> LOOPING

도움말 콘솔을 종료하려면 quit를 입력하십시오. 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() 함수 출력을 정의할 수 있습니다. 기본적으로 메서드 본문의 첫 번째 주석 문자열이 독스트링으로 사용됩니다. 세 개의 큰 따옴표로 둘러싸여 있습니다. 다음 코드가 포함된 파이썬 파일 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 독스트링 가이드는 적절한 도움말 문서화 방법에 대한 아이디어를 얻을 수 있는 좋은 장소입니다. 파이썬 콘솔에서 도움말 문서로 이 독스트링을 얻는 방법을 봅시다. 먼저 함수와 클래스 정의를 로드하려면 콘솔에서 이 스크립트를 실행해야 합니다. 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() 함수는 모듈, 클래스 및 함수에 대한 세부 정보를 얻는 데 매우 유용합니다. 사용법을 설명하기 위해 사용자 지정 클래스 및 함수에 대한 docstring을 정의하는 것이 항상 모범 사례입니다.

GitHub 리포지토리에서 전체 Python 스크립트와 더 많은 Python 예제를 확인할 수 있습니다.

참조: 공식 문서