본문 바로가기

컴퓨터/Python

__doc__ 속성과 help 함수

내장 함수는 모두 help를 통해 사용법을 알 수 있음

>>> help(print)

Help on built-in function print in module builtins:


print(...)

    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    

    Prints the values to a stream, or to sys.stdout by default.

    Optional keyword arguments:

    file: a file-like object (stream); defaults to the current sys.stdout.

    sep:  string inserted between values, default a space.

    end:  string appended after the last value, default a newline.



직접 만든 함수에도 help를 사용 가능함

>>> def plus(a, b):

return a + b


>>> help(plus)

Help on function plus in module __main__:

plus(a, b)



__doc__를 이용해 생성한 함수를 자세히 설명할 수 있음

>>> plus.__doc__ = "return the sum of parameter a, b"

>>> help(plus)

Help on function plus in module __main__:


plus(a, b)

    return the sum of parameter a, b




함수가 시작하는 부분에 "(쌍따옴표),"""(쌍따옴표3개)를 이용해 설명을 적으면
함수 객체가 생성될 때 자동으로 __doc__에 저장
>>> def factorial(x):
"""Return the factorial of n, an exact integer >= 0.
>>> factorial(6)
"""
if x == 1:
return 1
return x*factorial(x-1)

>>> help(factorial)
Help on function factorial in module __main__:

factorial(x)
    Return the factorial of n, an exact integer >= 0.
    >>> factorial(6)

'컴퓨터 > Python' 카테고리의 다른 글

제너레이터  (0) 2013.07.09
이터레이터(iterator)  (0) 2013.07.08
pass  (0) 2013.07.08
재귀적 함수 호출  (0) 2013.07.08
람다 함수  (0) 2013.07.08