Python

[python] docstring

yeonny_do 2023. 10. 25. 18:20

■ docstring 이란 ? 

  - 함수에 대한 설명문

  - help()와 __doc__를 이용해 출력할 수 있음

  - help()는 자세히 출력, __doc__은 docstring만 출력

help(print)

# 결과
#Help on built-in function print in module builtins:

#print(*args, sep=' ', end='\n', file=None, flush=False)
#    Prints the values to a stream, or to sys.stdout by default.
    
#    sep
#      string inserted between values, default a space.
#    end
#      string appended after the last value, default a newline.
#    file
#      a file-like object (stream); defaults to the current sys.stdout.
#    flush
#      whether to forcibly flush the stream.
print.__doc__

# 결과
# 'Prints the values to a stream, or to sys.stdout by default.\n\n  sep\n    string inserted between values, default a space.\n  end\n    string appended after the last value, default a newline.\n  file\n    a file-like object (stream); defaults to the current sys.stdout.\n  flush\n    whether to forcibly flush the stream.'

 

def echo(anything):
    'echo returns its input argument' #docstring
    return anything
echo('hello')
# 결과
# 'hello'

echo.__doc__
# 결과
# 'eco returns its input argument'

help(echo)
# 결과
#Help on function echo in module __main__:
#
#echo(anything)
#    eco returns its input argument

 

Reference : https://sesoc.tistory.com/236?category=1002032