Python Coding Convention

PEP8์ด ๊ถ๊ธˆํ•ด์ ธ์„œ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ๊ธ‰ ์ •๋ฆฌ

References: [์ฑ…] ํŒŒ์ด์ฌ ์ฝ”๋”ฉ์˜ ๊ธฐ์ˆ 

PEP8 ์ด๋ž€?

Python Enhancement Proposal 8 (PEP8)

  • Python code๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ• ์ง€ ์•Œ๋ ค์ฃผ๋Š” ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ

  • ์ฝ”๋”ฉ์„ ํ•  ๋•Œ ํŠน์ • ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜์ž๋Š” ์•ฝ์†

    • ์ผ๊ด€์„ฑ ์žˆ๋Š” ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋”์šฑ ์‰ฌ์›Œ์ง€๊ณ  ๊ฐ€๋…์„ฑ๋„ ๋†’์•„์ง„๋‹ค!

  • python.org ์— ์ž์„ธํžˆ ์ •๋ฆฌ๋˜์–ด ์žˆ์Œ!

PEP8 ์ค‘ ๋ฐ˜๋“œ์‹œ ๋”ฐ๋ผํ•˜์•ผ ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ทœ์น™

1. Whitespace

Python์—์„œ whitespace๋Š” ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜๋ฏธ๊ฐ€ ์žˆ์Œ

ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ํŠนํžˆ ์ฝ”๋“œ์˜ ๋ช…๋ฃŒ์„ฑ ๋•Œ๋ฌธ์— whitespace์˜ ์˜ํ–ฅ์— ๋ฏผ๊ฐํ•˜๋‹ค!

  • Tab์ด ์•„๋‹Œ Space๋กœ ๋“ค์—ฌ์“ด๋‹ค

  • ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜๋ฏธ ์žˆ๋Š” ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ๊ฐ ์ˆ˜์ค€๋งˆ๋‹ค space 4๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

  • ํ•œ ์ค„์˜ ๋ฌธ์ž ๊ธธ์ด๊ฐ€ 79์ž ์ดํ•˜์—ฌ์•ผ ํ•œ๋‹ค

  • ํ‘œํ˜„์‹์ด ๊ธธ์–ด์„œ ๋‹ค์Œ ์ค„๋กœ ์ด์–ด์ง€๋ฉด ์ผ๋ฐ˜์ ์ธ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์— ์ถ”๊ฐ€๋กœ Space 4๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

  • ํŒŒ์ผ์—์„œ ํ•จ์ˆ˜์™€ class๋Š” ๋นˆ ์ค„ ๋‘ ๊ฐœ๋กœ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•œ๋‹ค

  • Class์—์„œ method๋Š” ๋นˆ ์ค„ ํ•˜๋‚˜๋กœ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•œ๋‹ค

  • List index, ํ•จ์ˆ˜ ํ˜ธ์ถœ, keyword ์ธ์ˆ˜ ํ• ๋‹น ์—๋Š” space๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

2. Naming Conventions

PEP 8 ์€ ์–ธ์–ด์˜ ๋ถ€๋ถ„๋ณ„๋กœ ๋…์ž์ ์ธ ๋ช…๋ช… ๊ทœ์น™์„ ์ œ์•ˆํ•œ๋‹ค

์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ ๊ฐ ์ด๋ฆ„์— ๋Œ€์‘ํ•˜๋Š” ํƒ€์ž…์„ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ค€๋‹ค!

  • ํ•จ์ˆ˜, ๋ณ€์ˆ˜, ์†์„ฑ์€ lowercase_unserscore ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค

  • protected instance ์†์„ฑ์€ _leading_underscore ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค

  • private instance ์†์„ฑ์€ __double_leading_underscore ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค

  • Class์™€ Exception์€ CaplitalizeWord ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค

  • Module ์ˆ˜์ค€์˜ tkdtnsms ALL_CAPS ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค

  • Class instance์˜ method์—์„œ ์ฒซ ๋ฒˆ์งธ parameter (ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ) ์˜ ์ด๋ฆ„์„ self๋กœ ์ง€์ •ํ•œ๋‹ค

  • Class method์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ parameter (ํ•ด๋‹น class๋ฅผ ์ฐธ์กฐ)์˜ ์ด๋ฆ„์„ cls ๋กœ ์ง€์ •ํ•œ๋‹ค

3. ํ‘œํ˜„์‹๊ณผ ๋ฌธ์žฅ

ํŒŒ์ด์ฌ์˜ ๊ณ„๋ช… (The Zen of Python)์—๋Š” "์–ด๋–ค ์ผ์„ ํ•˜๋Š” ํ™•์‹คํ•œ ๋ฐฉ๋ฒ•์ด (๋  ์ˆ˜ ์žˆ์œผ๋ฉด ํ•˜๋‚˜๋งŒ) ์žˆ์–ด์•ผ ํ•œ๋‹ค" ๋Š” ํ‘œํ˜„์ด ์žˆ๋‹ค

PEP 8์€ ํ‘œํ˜„์‹๊ณผ ๋ฌธ์žฅ์˜ ๋ณธ๋ณด๊ธฐ๋กœ ์ด ์Šคํƒ€์ผ์„ ์ •๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค

  • ๊ธ์ • ํ‘œํ˜„์‹์˜ ๋ถ€์ • ( if not a is b ) ๋Œ€์‹ ์— ์ธ๋ผ์ธ ๋ถ€์ • ( if a is not b ) ์„ ์‚ฌ์šฉํ•œ๋‹ค

  • ๊ธธ์ด๋ฅผ ํ™•์ธ ( if len(somelist) == 0 ) ํ•˜์—ฌ ๋นˆ ๊ฐ’( [] or '' ) ์„ ํ™•์ธํ•˜์ง€ ์•Š๋Š”๋‹ค

    • if not somelist ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋นˆ ๊ฐ’์€ ์•”์‹œ์ ์œผ๋กœ False๊ฐ€ ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค

    • ๋น„์–ด์žˆ์ง€ ์•Š์€ ๊ฐ’์—๋„ ๊ฐ™์€ ๋ฐฉ์‹์ด ์ ์šฉ๋œ๋‹ค!

      • ๋น„์–ด์žˆ์ง€ ์•Š์€ ๊ฐ’์€ if somelist ๋ฌธ์ด ์•”์‹œ์ ์œผ๋กœ True ๊ฐ€ ๋œ๋‹ค

  • ํ•œ ์ค„๋กœ ๋œ if ๋ฌธ, for์™€ while loop, except ๋ณตํ•ฉ๋ฌธ ์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค

    • ์ด๋Ÿฐ ๋ฌธ์žฅ์€ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋‚˜๋ˆ„์–ด์„œ ๋ช…๋ฃŒํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ!

  • ํ•ญ์ƒ ํŒŒ์ผ์˜ ๋งจ ์œ„์— import๋ฌธ์„ ๋†“๋Š”๋‹ค

  • module์„ import ํ•  ๋•Œ๋Š” ํ•ญ์ƒ module์˜ ์ ˆ๋Œ€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉฐ ํ˜„์žฌ ๋ชจ๋“ˆ์˜ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

    • ex) import foo ๊ฐ€ ์•„๋‹ˆ๋ผ from bar import foo ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ!

  • ์ƒ๋Œ€์ ์ธ import๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋ช…์‹œ์ ์ธ ๊ตฌ๋ฌธ์„ ์จ์„œ from . import foo ๋ผ๊ณ  ํ•œ๋‹ค

  • import๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ, third party ๋ชจ๋“ˆ, ์ž์‹ ์ด ๋งŒ๋“  ๋ชจ๋“ˆ ์ˆœ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•œ๋‹ค

    • ๊ฐ๊ฐ์˜ ํ•˜์œ„ section์—์„œ๋Š” alphabetic order๋กœ import ํ•œ๋‹ค

PEP8์„ ์ž˜ ๋”ฐ๋ฅด๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•

pylint์™€ PEP8 ์„ค์น˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ!

1. pylint

$ pip install pylint

2. PEP8

$ pip install pep8

Last updated