๊ด€๋ฆฌ ๋ฉ”๋‰ด

Doby's Lab

๋ฌธ์ž์—ด์„ ๋” ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์„๊นŒ? (Raw String, docstring, ๋ฌธ์„œํ™”) ๋ณธ๋ฌธ

Code about AI/Python

๋ฌธ์ž์—ด์„ ๋” ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์„๊นŒ? (Raw String, docstring, ๋ฌธ์„œํ™”)

๋„๋น„(Doby) 2024. 5. 4. 11:40

๐Ÿค” Problem

์˜ค๋Š˜ ํฌ์ŠคํŒ…์€ ์–ด๋–ค ๋ฌธ์ œ์ ์„ ๊ฒช์—ˆ๋‹ค๊ธฐ๋ณด๋‹ค๋Š” ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•œ ๊ฒƒ์—์„œ ์‹œ์ž‘ํ•œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋งŽ์ด ์ ‘ํ•œ ๋ฌธ๋ฒ•์ด์ง€๋งŒ, ์ด๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋‹ค๋ฃจ์–ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

์˜คํ”ˆ์†Œ์Šค๋ฅผ ๋ณด๋ฉด, ์ด๋Ÿฐ ์ฝ”๋“œ๋“ค์„ ๋งŽ์ด ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

class Model(nn.Module):
    r"""
    param(1): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    param(2): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    """

    def __init__(self, param1, param2):
        super().__init__()

 

์ฒ˜์Œ ํŒŒ์ด์ฌ์„ ๊ณต๋ถ€ํ•  ๋•Œ๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ 3๋ฒˆ ์“ฐ๋Š” ๊ฒƒ์ด ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์ด๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์‹œ๊ฐ„์ด ์ง€๋‚˜์„œ ๋‹ค์‹œ ์ €๊ฑธ ์ฐพ์•„๋ดค์„ ๋•Œ๋Š” docstring์ด๋ผ ๋ถˆ๋ฆฌ๊ณ  ์žˆ์—ˆ๊ณ , ๋ฌธ์ž์—ด ์•ž์— ๋ถ™๋Š” r์€ Raw String์ด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋”๋ผ๊ณ ์š”.

 

๊ทธ๋ž˜์„œ, ์ด ๋‘ ๊ฐ€์ง€ ๊ฐœ๋…์ด ์–ด๋–ค ๊ฒƒ์ธ์ง€ ๊ธฐ๋ก์„ ํ•ด๋‘๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ˜€ Solution

1๏ธโƒฃ docstring

docstring์€ Documentation String์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์ฝ”๋“œ๋ฅผ ๋ฌธ์„œํ™”ํ•  ๋•Œ, ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ์˜คํ”ˆ์†Œ์Šค์˜ ํด๋ž˜์Šค, ํ•จ์ˆ˜, ๋ชจ๋“ˆ, ๋ฉ”์„œ๋“œ๋ผ๋ฉด ๋ชจ๋‘ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๊ณ , ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ๊ฒƒ์„ ํ•„์š”๋กœ ํ•˜๋Š”์ง€ ๊ฐ€์ด๋“œ๋ผ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. docstring์ด ๊ทธ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

docstring์˜ ์œ„์น˜๋Š” ํด๋ž˜์Šค, ํ•จ์ˆ˜, ๋ฉ”์„œ๋“œ๋ผ๋ฉด ์„ ์–ธ ๋ฐ”๋กœ ์•„๋žซ์ค„์— ๋‚˜ํƒ€๋‚ด๊ณ , ๋ชจ๋“ˆ์ด๋ผ๋ฉด ์ฝ”๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์— ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ๋”ฐ์˜ดํ‘œ๋ฅผ 3๊ฐœ ์‚ฌ์šฉํ•ด์„œ ๋‚˜ํƒ€๋‚ด๋ฉด docstring ์„ ์–ธ์ด ๋๋‚ฉ๋‹ˆ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•ด์š”!

 

๊ทธ๋Ÿฌ๋ฉด, ์ด docstring(๋ฌธ์„œ)๋ฅผ ์–ด๋–ป๊ฒŒ ํ™•์ธํ• ๊นŒ์š”? ์ด๊ฒƒ๋„ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. docstring์„ ์ž‘์„ฑํ•œ ๋ชจ๋“ˆ, ํด๋ž˜์Šค, ํ•จ์ˆ˜, ๋ฉ”์„œ๋“œ์—๋Š” __doc__์ด๋ผ๋Š” ์†์„ฑ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ด ์†์„ฑ์ด ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์ € ์ถœ๋ ฅ๋งŒ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

model = Model()

print(type(model.__doc__))
print(model.__doc__)
print(Model.__doc__)

#Output
<class 'str'>

    param(1): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    param(2): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •


    param(1): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    param(2): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •

๊ทธ๋ฆฌ๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ docstring์˜ ํŠน์ดํ•œ ์ ์ด ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค.

docstring์ด ๋งŒ์•ฝ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ๋œ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค.

class Model(nn.Module):
    """
    param(1): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    param(2): ~~~ํ•  ๋•Œ, ~~~๋กœ ์ง€์ •
    'str1 ' "str2"
        tab1
            tab2
    """

์›๋ž˜์˜ ๋ฌธ์ž์—ด์„ ์„ ์–ธ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ค„ ๋„˜๊น€, ๋”ฐ์˜ดํ‘œ, ๊ณต๋ฐฑ(ํƒญ) ๊ด€๋ จ ๋ฌธ์ž๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฑ์Šฌ๋ž˜์‹œ๋กœ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, docstring์€ ๊ทธ๋Ÿฐ ๊ฒƒ์œผ๋กœ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ์ •๋ง ํ•˜๋‚˜์˜ ๋ฌธ์„œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉฐ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž ์—†์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ํŠน์ดํ•œ ์ ์ด์ž ๋ฌธ์„œํ™”๋ฅผ ํ•  ๋•Œ ํŽธํ•œ ์ ์ž…๋‹ˆ๋‹ค.

 

(+ ๊ทธ๋Ÿฌ๋ฉด '์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋กœ ๋‚˜ํƒ€๋‚ธ ์ค„ ๋„˜๊น€, ๋”ฐ์˜ดํ‘œ, ๊ณต๋ฐฑ(ํƒญ)์€ ๋ชป ํ•˜๋Š” ๊ฒƒ์ด๋ƒ?'๋ผ๊ณ  ์งˆ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชป ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ์ „ํžˆ ๊ทธ ๊ธฐ๋Šฅ๋“ค ๋˜ํ•œ ์ˆ˜ํ–‰์„ ํ•ฉ๋‹ˆ๋‹ค.)

2๏ธโƒฃ Raw String

docstring์€ ๋”ฐ์˜ดํ‘œ 3๊ฐœ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋งจ ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” ๋”ฐ์˜ดํ‘œ 3๊ฐœ ์•ž์— r์ด๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” docstring๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•˜๋ฉฐ, ์ด๋Š” Raw String์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

Raw String์˜ r์„ ๋”ฐ์„œ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œํ˜„ํ•œ ๋ฌธ๋ฒ•์œผ๋กœ, 'Raw String์ด๋ž€ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž(๋ฐฑ์Šฌ๋ž˜์‹œ)๋ฅผ ์ „๋ถ€ ๋ฌด์‹œํ•œ๋‹ค'๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

a = "abc\nabc"
b = r"abc\nabc"
print(a)
print(b)

#Output
abc 
abc
abc\nabc

๋ฌธ์ž์—ด b๊ฐ€ Raw String์„ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๊ฐ€ ์ ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์„ ๋ฌด์‹œํ•˜๊ณ  ๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ docstring + Raw String

๊ทธ๋ž˜์„œ ๋‘ ๋ฌธ๋ฒ•์€ ์ฝ”๋“œ์˜ ๋ฌธ์„œํ™”๋ฅผ ์œ„ํ•ด ์กฐํ•ฉ์„ ์‹œ์ผœ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, docstring ๋‹จ๋ฝ์—์„œ ๋งํ•œ ๊ฒƒ์ฒ˜๋Ÿผ docstring์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋Š” ๊ทธ๋Œ€๋กœ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์„œ์—์„œ ๋ฐฑ์Šฌ๋ž˜์‹œ ๊ด€๋ จ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๋‘ ์กฐํ•ฉ์„ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 


๐Ÿ“‚ Reference

https://wikidocs.net/16050

 

24. Docstring - ๋ฌธ์„œํ™”

- ํŒŒ์ด์ฌ์„ ์ ‘ํ•˜๋ฉด์„œ, ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ๋ฉ‹์ง€๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋˜ ๊ฒƒ ์ค‘์— ํ•˜๋‚˜ - docstring์„ ์ž‘์„ฑํ•˜๋ฉด, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์†์„ฑ์œผ๋กœ ์ ‘๊ทผํ• ์ˆ˜ ์žˆ์Œ. ## 1. docstring ์ด๋ž€ …

wikidocs.net

 

728x90