문서화 문자열
Coddy의 Clean Code - Python으로 더 나은 코드 작성하기 코스 레슨 — 28개 중 8번째.
문서 문자열(Document String) 또는 독스트링(Docstring)은 주로 모듈, 함수, 클래스 또는 메서드 정의의 첫 번째 문장으로 나타납니다.
예를 들어, 문자열을 """ 또는 '''로 감싸서 독스트링을 선언합니다.
def complex(real=0.0, imag=0.0):
"""
Form a complex number.
Parameters:
real (float) -- the real part (default 0.0)
imag (float) -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zero
...complex(real, imag) 함수를 어떻게 문서화하는지 살펴보세요.
문서 본문과
"""의 들여쓰기에 주목하세요.
이와 같이 독스트링을 사용한 후에는 __doc__ 속성을 사용하여 문서를 가져올 수 있습니다.
complex.__doc__다음과 같이 출력됩니다.
Form a complex number.
Parameters:
real (float) -- the real part (default 0.0)
imag (float) -- the imaginary part (default 0.0)챌린지
쉬움docstring 대신 블록 주석이 포함된 함수가 주어집니다.
여러분의 작업은 이를 유효한 docstring으로 교체하는 것입니다. 올바른 docstring 형식을 맞추기 위해 테스트 케이스를 확인하세요!
직접 해보기
def sum_binary(a, b):
# Calculate sum of two integers in binary formmated string
# Parameters:
# a (int) -- Integer number
# b (int) -- Another integer number
# Returns:
# binary_sum (str) -- The sum of a and b in binary format
sum = a + b
binary_sum = bin(sum)
return str(binary_sum)[1:] # Final formmating
print(sum_binary.__doc__)