๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป DEV/Python

[Python] DocStrings (๋…์ŠคํŠธ๋ง) ์ž‘์„ฑ

by Rising Oneโ˜… 2023. 5. 25.
728x90
๋ฐ˜์‘ํ˜•
SMALL

 

Python ๋Œ€ํ‘œ์ด๋ฏธ์ง€

 

ํ•„์š”์„ฑ 

ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•ด ์ž‘์—…ํ•  ํ•จ์ˆ˜(Function)๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋‹ค๋ฃจ๋˜ ์ค‘, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” "Python ๋‚ด DocString"์„ ์•Œ๊ฒŒ ๋์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋ฉด, ์ฝ”๋“œํŽธ์ง‘๊ธฐ ์ฐฝ์— ๋ฉ๋‹ฌ์•„ ์•ˆ๋‚ด๋˜๋Š” ํ•จ์ˆ˜ ์„ค๋ช…๋ฌธ๊ตฌ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ฅผ ํ†ตํ•ด ํ˜‘์—…์‹œ ์†Œํ†ต ๋˜ํ•œ ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™์•„ ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 

 


์ ‘๊ทผ (์š”์•ฝ)

 

  1. Docstring ์ด๋ž€?

      (1.1) ์ •์˜, ํŠน์ง•

 

  2. Python์—์„œ์˜ ์ฝ”๋“œ ์˜ˆ์‹œ

      (2.1). (One-line / Mulit-line) Docstring

      (2.2). ํ•จ์ˆ˜ ์ƒ์„ฑ-์„ ์–ธ

      (2.3). ์ฝ”๋“œํŽธ์ง‘๊ธฐ์—์„œ์˜ Python ์†์„ฑ์— ์ ‘๊ทผ


๋ฐฉ๋ฒ•


  1. Docstring ์ด๋ž€?

 

      (1.1) ์ •์˜, ํŠน์ง•(์ž‘์„ฑ๋ฒ•)

 

์ •์˜ : Docstring์ด๋ž€, ๋ชจ๋“ˆ, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋˜๋Š” ๋ฉ”์†Œ๋“œ ์ •์˜์˜ ์ฒซ ๋ฒˆ์งธ ๋ช…๋ น๋ฌธ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ž…๋‹ˆ๋‹ค.

 

ํŠน์ง• 1. ํฌ๊ฒŒ "One-line Docstrings"๊ณผ "Multi-line Docstring"์œผ๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠน์ง• 2. def ํ•จ์ˆ˜๋ช…(): ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋‹ค์Œ ํ–‰์— """  """ 3์Œ์˜ ํฐ๋”ฐ์˜ดํ‘œ(double-quote)๋ฅผ ์ž‘์„ฑํ•ด ์‚ฌ์šฉ์ค€๋น„๋ฅผ ํ•œ๋‹ค.

ํŠน์ง• 3. 3์Œ์˜ ํฐ๋”ฐ์˜ดํ‘œ(double-quote) """Docstring ๋‚ด์šฉ""" ์‚ฌ์ด์— ์ž‘์„ฑํ•  ๋‚ด์šฉ์„ ๊ธฐ์ž…ํ•œ๋‹ค.

ํŠน์ง• 4. ํƒ€ ํ–‰์—์„œ ํ•จ์ˆ˜๋ช…(), ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด๋ณด๋ฉด ์ฝ”๋“œํŽธ์ง‘๊ธฐ ๋‚ด์— ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ Docstring์ด ๋“œ๋Ÿฌ๋‚œ๋‹ค.  

 


  2. Python์—์„œ์˜ ์ฝ”๋“œ ์˜ˆ์‹œ

 

      (2.1). (One-line / Mulit-line) Docstring

# One-line Docstring

def function(a, b):
    """function(a, b) -> list"""
# Multi-line Docstring

def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)
    """
    if imag == 0.0 and real == 0.0:
        return complex_zero
    ...

      (2.2). ํ•จ์ˆ˜ ์ƒ์„ฑ-์„ ์–ธ

# ์‹ค์ œ ์˜ˆ์‹œ
# title() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋Š” ์˜ˆ์ œ

def format_name(f_name, l_name):
  """
  format_name ํ•จ์ˆ˜๋Š” ๋‘ ์ธ์ž๋ฅผ ๋ฐ›์•„์„œ ์ด๋ฅผ title case version์œผ๋กœ
  ๋งŒ๋“ค์–ด return ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  """
  
  if f_name == "" or l_name == "": 
    return "You didn't provide valid inputs."
  formated_f_name = f_name.title()
  formated_l_name = l_name.title()

  return f"Result: {formated_f_name} {formated_l_name}"

 

      (2.3). ์ฝ”๋“œํŽธ์ง‘๊ธฐ์—์„œ์˜ Python ์†์„ฑ์— ์ ‘๊ทผ

 

์œ„์ฒ˜๋Ÿผ, Docstring์„ ์ž‘์„ฑํ•ด์ค€ ํ•จ์ˆ˜๋ฅผ ํƒ€ ํ–‰์—์„œ ํƒ€์ดํ•‘ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ํ•ด๋‹น ํ•จ์ˆ˜์— ๋Œ€ํ•œ Docstring์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด๋ฅผ python์˜ ์†์„ฑ์— ์ ‘๊ทผํ–ˆ๋‹ค๋Š” ํ‘œํ˜„์œผ๋กœ ์ผ๋‹ค.)

 


์ด์ƒ์œผ๋กœ, Python ๋‚ด Docstring์„ ์ž‘์„ฑ ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์งˆ๋ฌธ์„ ๋Œ“๊ธ€์„ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค^^!

์˜ค๋Š˜๋„ ํŒŒ์ดํŒ…์ž…๋‹ˆ๋‹ค! 

728x90
๋ฐ˜์‘ํ˜•
LIST

๋Œ“๊ธ€