From fafedbcf5183eee99b0102e349fd376ca6739e41 Mon Sep 17 00:00:00 2001 From: LEE DONG GUN Date: Sun, 8 Oct 2023 19:42:37 +0900 Subject: [PATCH] =?UTF-8?q?[6e7639a]=20=EB=B2=88=EC=97=AD=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20=F0=9F=92=AC=20(#27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * merge 6e7639a ๐Ÿ›ฐ * [6e7639a] ๋ฒˆ์—ญ * [6e7639a] build --- Google Python Style Guide kor.md | 243 ++++---- .../2.13 Properties.md" | 70 +-- ...4 True, False \355\217\211\352\260\200.md" | 7 +- ...224\354\204\234\353\223\234 Decorators.md" | 4 +- .../2.2 import.md" | 8 +- ...0 \355\214\214\354\235\264\354\215\254.md" | 37 +- .../2.3 package.md" | 2 +- ...10\354\231\270\354\262\230\353\246\254.md" | 2 +- .../ReadMe.md" | 131 ++--- ...0 \353\254\270\354\236\220\354\227\264.md" | 2 +- ...1\352\267\274 \354\240\234\354\226\264.md" | 16 +- ...4\354\204\235 \353\260\251\353\262\225.md" | 71 +-- .../3.5 \353\271\210 \354\244\204.md" | 4 + ...\274\354\204\235\352\263\274 docstring.md" | 15 +- .../ReadMe.md" | 108 ++-- Original.md | 556 ++++++++---------- 16 files changed, 552 insertions(+), 724 deletions(-) diff --git a/Google Python Style Guide kor.md b/Google Python Style Guide kor.md index 7983c56..68e036b 100644 --- a/Google Python Style Guide kor.md +++ b/Google Python Style Guide kor.md @@ -23,7 +23,7 @@ * [2.17 ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ Decorators](#s2.17-function-and-method-decorators) * [2.18 ์Šค๋ ˆ๋“œ](#s2.18-threading) * [2.19 ๊ฐ•ํ•œ ๊ธฐ๋Šฅ](#s2.19-power-features) - * [2.20 Modern Python : Python 3 ๊ทธ๋ฆฌ๊ณ  from, \_\_future\_\_, imports](#s2.20-modern-python) + * [2.20 Modern Python : from, \_\_future\_\_, imports](#s2.20-modern-python) * [2.21 Type ์ฃผ์„](#s2.21-typed-code) - [3. Python ์Šคํƒ€์ผ ๊ทœ์น™](#s3) * [3.1 Semicolons](#s3.1-semicolons) @@ -48,7 +48,7 @@ * [3.12 TODO Comments](#s3.12-todo-comments) * [3.13 import ํ˜•์‹](#s3.13-imports-formatting) * [3.14 Statements](#s3.14-statements) - * [3.15 ์ ‘๊ทผ ์ œ์–ด](#s3.15-access-control) + * [3.15 ์ ‘๊ทผ ์ œ์–ด](#getters-and-setters) * [3.16 ๋„ค์ด๋ฐ](#s3.16-naming) + [3.16.1 ํ”ผํ•ด์•ผ ํ•  ์ด๋ฆ„](#s3.16.1-names-to-avoid) + [3.16.2 Naming Conventions](#s3.16.2-naming-conventions) @@ -173,11 +173,7 @@ ### 2.2 Imports -- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์„ ๋Œ€์ƒ์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ณ  ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ - [typing ๋ชจ๋“ˆ](#s3.19.12-imports), - [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions), - ๊ทธ๋ฆฌ๊ณ  [six.moves module](https://six.readthedocs.io/#module-six.moves) - ์„ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค. +- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์— ์‚ฌ์šฉํ•˜๊ณ  ๊ฐœ๋ณ„ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ [typing ๋ชจ๋“ˆ](#s3.19.12-imports), [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions)์—์„œ ๊ฐ€์ ธ์˜จ ํด๋ž˜์Šค ๋ฐ [six.moves module](https://six.readthedocs.io/#module-six.moves)์—์„œ์˜ ๋ฆฌ๋””๋ ‰์…˜์€ ์ด ๊ทœ์น™์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค. @@ -216,7 +212,8 @@ - import๋œ๊ฒƒ๋“ค๊ณผ ๊ด€๋ จ์žˆ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. - ๋ชจ๋“ˆ์ด ๊ฐ™์€ ํŒจํ‚ค์ง€์— ์žˆ๋”๋ผ๋„ ์ „์ฒด ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. +- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. + --- @@ -262,7 +259,7 @@ FLAGS = flags.FLAGS ``` - - ๋ถ€์ ์ ˆํ•œ ์˜ˆ (์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.) + - ๋ถ€์ ์ ˆํ•œ ์˜ˆ _(์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.)_ ```python # Unclear what module the author wanted and what will be imported. The actual @@ -356,7 +353,7 @@ - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํŒจํ‚ค์ง€๋Š” ๊ณ ์œ ์˜ ์˜ˆ์™ธ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์‚ฌ์šฉํ•˜๋Š” ๋™์•ˆ, ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค(exception class)๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋ง๋”๋“ฌ์ด(stutter)๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). +- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋˜ํ’€์ด๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). - ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ์“ฐ๋ ˆ๋“œ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ชฝ ๋ธ”๋ก์— ์žˆ์ง€์•Š์œผ๋ฉด ์ ˆ๋Œ€ ํฌ๊ด„์ ์ธ `except:`๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ `Exception`, `StandardError`์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. (๊ทธ๋ฆฌ๊ณ  ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.) Python์€ ์ด์™€ ๊ด€๋ จํ•ด์„œ ๋งค์šฐ ๊ด€์šฉ์ ์ด๋ฉฐ `except:` ๋ชจ๋“  ์˜คํƒˆ์ž๋ฅผ ๋น„๋กฏํ•˜์—ฌ, sys.exit() ํ˜ธ์ถœ, Ctrl+C๋กœ ์ธํ•œ ์ธํ„ฐ๋ŸฝํŠธ, ์œ ๋‹›ํ…Œ์ŠคํŠธ ์‹คํŒจ์™€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹น์‹ ์ด ํฌ์ฐฉ์„ ์›ํ•˜์ง€ ์•Š์•˜๋˜ ๋‹ค๋ฅธ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋“ค๊นŒ์ง€ ๋ชจ๋‘ ์žก์•„๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์ฝ”๋“œ์ƒ์—์„œ `try`/`except` ๋ธ”๋ก์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™”์‹œํ‚ค์„ธ์š”. `try`๋ฌธ์˜ ๋‚ด๋ถ€๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์˜ˆ์™ธ๋Š” ๋‹น์‹ ์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜์ง€ ์•Š์•˜๋˜ ์ฝ”๋“œ์— ์˜ํ•ด ์ ์  ๋” ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, `try`/`except` ๋ธ”๋ก์€ ์ง„์งœ ๊ฒ€์ถœํ•ด์•ผ ํ•  ์—๋Ÿฌ๋ฅผ ๊ฐ€๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. - ์˜ˆ์™ธ๊ฐ€ `try` ๋ธ”๋ก์—์„œ ๋ฐœ์ƒํ•˜๋˜ ์•ˆํ•˜๋˜ `finally`์ ˆ์€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์ด๊ฑด ๊ฐ€๋” ๊น”๋”ํžˆ ํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ํŒŒ์ผ์„ ๋‹ซ์„ ๋•Œ ๊ฐ€ ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค. @@ -776,80 +773,44 @@ ### 2.13 Properties -- `Properties`์„ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ๋ณดํ†ต ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ธ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž๋‚˜ `setter` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- `Properties`๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์ด๋‚˜ ๋กœ์ง์ด ํ•„์š”ํ•œ attribute์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `Properties` ๊ตฌํ˜„์€ ๋ณดํ†ต์˜ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ธฐ๋Œ€์™€ ์ผ์น˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. #### 2.13.1 ์ •์˜ -- ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ํ•  ๋•Œ ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. #### 2.13.2 ์žฅ์  -- ๊ฐ„๋‹จํ•œ ์†์„ฑ ์ ‘๊ทผ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์ธ `get`, `set` ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๊ฐ€๋…์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +- getter, setter method ํ˜ธ์ถœ ๋Œ€์‹  attribute ์ ‘๊ทผ ๋ฐ ํ• ๋‹น API๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. +- attribute๋ฅผ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - [๋Š๊ธ‹ํ•œ ๊ณ„์‚ฐ๋ฒ•](https://ko.wikipedia.org/wiki/๋Š๊ธ‹ํ•œ_๊ณ„์‚ฐ๋ฒ•)์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. -- ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ [Pythonic](https://github.com/Yosseulsin-JOB/Google-Python-Style-Guide-kor/wiki/2.13-properties#pythonic)์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. -- ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ, trivial ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋Š” ์ง์ ‘ ๋ณ€์ˆ˜ ์ ‘๊ทผ์ด ํ•ฉ๋ฆฌ์ ์ผ ๋•Œ ์†์„ฑ ์šฐํšŒ๋ฅผ ํ—ˆ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํŒŒ๊ดดํ•˜์ง€ ์•Š๊ณ  ๋ฏธ๋ž˜์— ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. +- ๋‚ด๋ถ€๊ฐ€ class ์‚ฌ์šฉ์ž์™€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ฐœ์ „ํ•  ๋•Œ ํด๋ ˆ์Šค์˜ public interface๋ฅผ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. #### 2.13.3 ๋‹จ์  -- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.13.4 ๊ฒฐ๋ก  -- ์ƒˆ ์ฝ”๋“œ์—์„œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž ๋˜๋Š” `setter` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ์€ `@property` [decorator](#s2.17-function-and-method-decorators)๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ ์ž์ฒด๊ฐ€ ์žฌ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์†์„ฑ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋„๋กํ•˜๋ ค๋ฉด ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค([template method design pattern](https://en.wikipedia.org/wiki/Template_method_pattern)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.). -- ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ - - ```python - import math - class Square: - """๋‘ ๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์ง„ ์‚ฌ๊ฐํ˜•: ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋ฉด์ (area)๊ณผ ์ฝ๊ธฐ์ „์šฉ์ธ ๋‘˜๋ ˆ(perimeter) - - ์‚ฌ์šฉ๋ฐฉ๋ฒ•: - >>> sq = Square(3) - >>> sq.area - 9 - >>> sq.perimeter - 12 - >>> sq.area = 16 - >>> sq.side - 4 - >>> sq.perimeter - 16 - """ - - def __init__(self, side: float): - self.side = side - - @property - def area(self) -> float: - """์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.""" - return self._get_area() - - @area.setter - def area(self, area: float): - self._set_area(area) - - def _get_area(self) -> float: - """'๋ฉด์ '์†์„ฑ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์ ‘๊ทผ์ž์ž…๋‹ˆ๋‹ค.""" - return self.side ** 2 - - def _set_area(self, area: float): - """'๋ฉด์ ' ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์„ค์ •์ž์ž…๋‹ˆ๋‹ค.""" - self.side = math.sqrt(area) - - @property - def perimeter(self) -> float: - return self.side * 4 - ``` +- Properties๋Š” ํ—ˆ์šฉํ•˜์ง€๋งŒ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์ธ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ๊ธฐ๋Œ€์™€ ์ผ์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค + - ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” [getters and setters](#getters-and-setters)๊ทœ์น™์„ ๋”ฐ๋ฅด์„ธ์š”. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹จ์ˆœํžˆ attribute๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + - ๊ณ„์‚ฐ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ property๋Š” ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ([๋Œ€์‹  attribute๋ฅผ public์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.](#getters-and-setters)) +- ์ด์— ๋น„ํ•ด attribute ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ์‚ฌ์†Œํ•œ ํŒŒ์ƒ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. + - ๋กœ์ง์€ ๊ฐ„๋‹จํ•˜๊ณ  ๋†€๋ž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- Properties๋Š” `@property` [decorator](#s2.17-function-and-method-decorators)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- property descriptor๋ฅผ ์ˆ˜๋™์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ [power feature](#power-features)๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. +- ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. subclass๊ฐ€ ์žฌ์ •์˜ํ•˜๊ณ  ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ณ„์‚ฐ ๋„๊ตฌ๋กœ properties๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. --- @@ -896,9 +857,6 @@ if not users: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo == 0: - self.handle_zero() - if i % 10 == 0: self.handle_multiple_of_ten() @@ -913,9 +871,6 @@ if len(users) == 0: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo is not None and not foo: - self.handle_zero() - if not i % 10: self.handle_multiple_of_ten() @@ -924,6 +879,7 @@ ``` - `'0'`(์ฆ‰, `0` ๋ฌธ์ž์—ด)์€ ์ฐธ์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- Numpy ๋ฐฐ์—ด์€ ์•”์‹œ์  boolean ์ปจํ…์ŠคํŠธ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `np.array`์˜ ๋น„์–ด ์žˆ์Œ์„ ํ…Œ์ŠคํŠธํ•  ๋•Œ `.size` attribute๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. (e.g. `if not users.size`) --- @@ -1026,7 +982,9 @@ #### 2.17.3 ๋‹จ์  -- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” importํ•  ๋•Œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ Decorator ์ฝ”๋“œ๋Š” ํšŒ๋ณต์ด ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” object ์ •์˜ ์‹œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. module-level ๊ฐ์ฒด(classes, module functions, ...)์˜ ๊ฒฝ์šฐ importํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. +- Decorator ์ฝ”๋“œ ์˜ค๋ฅ˜๋Š” ๋ณต๊ตฌ๊ฐ€ ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. @@ -1081,28 +1039,30 @@ --- -### 2.20 Modern Python : Python 3 ๊ทธ๋ฆฌ๊ณ  from, \_\_future\_\_, imports +### 2.20 Modern Python : from, \_\_future\_\_, imports -- Python 3 ๋ฒ„์ „์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค! ์•„์ง ํ”„๋กœ์ ํŠธ์— Python 3์„ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ๋Š” ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๋ชจ๋“  ์ฝ”๋“œ๋Š” ํ˜ธํ™˜๋˜๋„๋ก ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์— Python 3์— ๋”ฐ๋ผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.) +- ์ƒˆ๋กœ์šด ์–ธ์–ด ๋ฒ„์ „ ์˜๋ฏธ ์ฒด๊ณ„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ์ด์ „ ๋Ÿฐํƒ€์ž„ ๋‚ด์—์„œ ํŒŒ์ผ ๋‹จ์œ„๋กœ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ํ–ฅํ›„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋’ค์— ์ œ์–ด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + (New language version semantic changes may be gated behind a special future import to enable them on a per-file basis within earlier runtimes.) #### 2.20.1 ์ •์˜ -- Python 3๋Š” Python์–ธ์–ด์—์„œ ์ค‘์š”ํ•œ ๋ณ€ํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ๋Š” 2.7 ๋ฒ„์ „์„ ์—ผ๋‘ํ•˜์—ฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. -- Python3์—์„œ ์ˆ˜์ •์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž˜ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝ”๋“œ์˜ ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ช‡๋ช‡ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +- `from __future__ import`๋ฌธ์„ ํ†ตํ•ด ๋ณด๋‹ค ํ˜„๋Œ€์ ์ธ ๊ธฐ๋Šฅ ์ค‘ ์ผ๋ถ€๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์˜ˆ์ƒ๋˜๋Š” ํ–ฅํ›„ Python ๋ฒ„์ „์˜ ๊ธฐ๋Šฅ์„ ์กฐ๊ธฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.20.2 ์žฅ์  -- Python 3๋ฅผ ์—ผ๋‘ํ•ด ๋‘๊ณ  ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋Š” ๋ช…ํ™•ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์˜ ๋ชจ๋“  ์˜์กด์„ฑ์ด Python 3์—์„œ ์‹คํ–‰ํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. +- ์ด๋Š” ํ˜ธํ™˜์„ฑ์„ ์„ ์–ธํ•˜๊ณ  ํ•ด๋‹น ํŒŒ์ผ ๋‚ด์—์„œ ํšŒ๊ท€๋ฅผ ๋ฐฉ์ง€ํ•˜๋ฉด์„œ ํŒŒ์ผ๋ณ„๋กœ ๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋Ÿฐํƒ€์ž„ ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ๋” ์›ํ™œํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ ์ž…์ค‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- ์ตœ์‹  ์ฝ”๋“œ๋Š” ํ–ฅํ›„ ๋Ÿฐํƒ€์ž„ ์—…๊ทธ๋ ˆ์ด๋“œ ์ค‘์— ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์  ๋ถ€์ฑ„๊ฐ€ ์ถ•์ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค. #### 2.20.3 ๋‹จ์  -- ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์ถ”๊ฐ€๋œ boilerplate๊ฐ€ ์ถ”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ์„ importํ•˜๋Š” ๊ฒƒ์€ ์ด๋ก€์ ์ž…๋‹ˆ๋‹ค. +- ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ feature ๋ฌธ์„ ๋„์ž…ํ•˜๊ธฐ ์ „์—๋Š” ๋งค์šฐ ์˜ค๋ž˜๋œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. @@ -1110,7 +1070,16 @@ ##### from \_\_future\_\_ imports -- `from __future__ import` ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋Š” ๋‹ค์Œ ์‚ฌํ•ญ์ด ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋ฉฐ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ํ˜ธํ™˜๋˜๋„๋ก ์—…๋ฐ์ดํŠธ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +- `from __future__ import`๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ฃผ์–ด์ง„ ์†Œ์ŠคํŒŒ์ผ์—์„œ ๋”์šฑ ํ˜„๋Œ€์ ์ธ Python ๊ตฌ๋ฌธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `__future__` import ๋’ค์— ๊ธฐ๋Šฅ์ด ์ˆจ๊ฒจ์ ธ ์žˆ๋Š” ๋ฒ„์ „์—์„œ ๋” ์ด์ƒ ์‹คํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ค„์„ ์ž์œ ๋กญ๊ฒŒ ์ œ๊ฑฐํ•˜์„ธ์š”. +- 3.7 ์ด์ƒ์ด ์•„๋‹Œ 3.5 ์ด์ „ ๋ฒ„์ „์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์—์„œ ๊ฐ€์ ธ์˜ฌ ๊ฒฝ์šฐ + + ```python + from __future__ import generator_stop + ``` + +- 2.7 ๋ฒ„์ „์„ ๊ณ„์† ์ง€์›ํ•ด์•ผํ•˜๋Š” ๋ถ€๋‹ด์ด ์žˆ๋Š” ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ ```python from __future__ import absolute_import @@ -1118,13 +1087,17 @@ from __future__ import print_function ``` -- `import`์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [absolute imports](https://www.python.org/dev/peps/pep-0328/), [`/` division behavior](https://www.python.org/dev/peps/pep-0238/), [the `print` function](https://www.python.org/dev/peps/pep-3105/)์„ ์ฐธ์กฐํ•˜์„ธ์š”. -- ์ด๋Ÿฌํ•œ import๋Š” ํ˜„์žฌ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. ๋ชจ๋“  ํŒŒ์ผ์— ํ•ญ์ƒ ํ–ฅํ›„ import๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‚˜์ค‘์— ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ํŽธ์ง‘ํ•˜๋Š” ๋™์•ˆ ์žŠ์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- ๋‹ค๋ฅธ `from __future__` import ๋ช…์„ธ๋„ ์žˆ์œผ๋‹ˆ ์•Œ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `unicode_literals`๋Š” ํŒŒ์ด์ฌ 2.7 ๋‚ด ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ๋„์ž…๋˜๋Š” ์•”๋ฌต์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ ๋•Œ๋ฌธ์— ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ๊ณ ์‚ฌํ•ญ์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋Š” ํ•„์š”์— ๋”ฐ๋ผ `b''`, `u''` ๋ฐ”์ดํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด literal๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Python future statement definitions](https://docs.python.org/3/library/__future__.html) ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด์„ธ์š”. +- ์ฝ”๋“œ๊ฐ€ ์ถฉ๋ถ„ํžˆ ํ˜„๋Œ€์ ์ธ ํ™˜๊ฒฝ์—์„œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ํ™•์‹ ์ด ๋“ค ๋•Œ๊นŒ์ง€ ์ด๋Ÿฌํ•œ import๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. +- ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ํŠน์ • ํ–ฅํ›„ import๋ฅผ ํ†ตํ•ด ํ™œ์„ฑํ™”๋˜๋Š” ๊ธฐ๋Šฅ์„ ํ˜„์žฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ํŒŒ์ผ์— ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์œ ์ง€ํ•˜๋ฉด ๋‚˜์ค‘์— ์ฝ”๋“œ๊ฐ€ ์ด์ „ ๋™์ž‘์— ๋”ฐ๋ผ ์‹ค์ˆ˜๋กœ ์ˆ˜์ •๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋‹ค๋ฅธ `from __future__` import ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- 2.7๋ฒ„์ „ ๋‚ด์— ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ๋„์ž…๋œ ์•”์‹œ์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ๋กœ ์ธํ•ด ํ™•์‹คํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— 2.7๋ฒ„์ „์— ๋Œ€ํ•œ ๊ถŒ์žฅ ์‚ฌํ•ญ์— `unicode_literals`๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. +- ๋Œ€๋ถ€๋ถ„์˜ ์ด์ค‘ ๋ฒ„์ „ 2-3 ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ `b''` ์™€ `u''` q๋ฐ”ํŠธ์™€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์•˜์Šต๋‹ˆ๋‹ค. ##### six, future ๊ทธ๋ฆฌ๊ณ  past ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ -- ํ”„๋กœ์ ํŠธ๊ฐ€ Python 2, 3 ๋ชจ๋‘ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ ํ•ฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ์ ํŠธ๊ฐ€ ์—ฌ์ „ํžˆ Python 2, 3 ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ ํ•ฉํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š” ๋Œ€๋กœ [six](https://pypi.org/project/six/), [future](https://pypi.org/project/future/), ๋ฐ [past](https://pypi.org/project/past/) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. --- @@ -1417,6 +1390,10 @@ - `def` ์ค„ ์ดํ›„์—๋Š” ๋นˆ ์ค„์ด ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ ์‚ฌ์ด์— ๊ฐœ๋ฐœ์ž์˜ ํŒ๋‹จํ•˜์— ์ ์ ˆํ•˜๊ฒŒ ํ•œ ๊ฐœ์˜ ๋นˆ ์ค„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๋นˆ ์ค„์„ ์ •์˜์— ๊ณ ์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ๋ฐ ๋ฉ”์„œ๋“œ ์ •์˜ ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๊ด€๋ จ ์ฃผ์„์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- comment๊ฐ€ Docstring์˜ ์ผ๋ถ€๋กœ ๋” ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. + --- @@ -1620,6 +1597,10 @@ - ๋ฐ˜ํ™˜๊ฐ’์˜ ์ž๋ฃŒํ˜•๊ณผ ์˜๋ฏธ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜๊ฐ€ None๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์ด ์„น์…˜์€ ํ•„์š”์—†์Šต๋‹ˆ๋‹ค. - ๋˜ํ•œ ๋งŒ์•ฝ docstring์ด Returns ๋‚˜ Yields๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜(e.g. `"""Returns row from Bigtable as a tuple of strings."""`) ์ถฉ๋ถ„ํ•œ ์„ค๋ช…์ด ์ œ๊ณต๋œ๋‹ค๋ฉด ์ƒ๋žต ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Tuple ๋ฐ˜ํ™˜ ๊ฐ’์„ ๊ฐœ๋ณ„ ์ด๋ฆ„์ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ฐ˜ํ™˜ ๊ฐ’์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ž์ฃผ ๋ฌธ์„œํ™”ํ•˜๋Š” 'NumPy style' ([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html))์„ ๋ชจ๋ฐฉํ•˜์ง€ ๋งˆ์„ธ์š”. (Tuple๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.) +- ๋Œ€์‹ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ธฐ์ˆ ํ•˜์„ธ์š”. + - "Returns a tuple (mat_a, mat_b), where mat_a is ..., and ...". +- Docstring์˜ ๋ณด์กฐ ์ด๋ฆ„์€ ํ•จ์ˆ˜ ๋ณธ๋ฌธ์— ์‚ฌ์šฉ๋œ ๋‚ด๋ถ€ ์ด๋ฆ„๊ณผ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. (ํ•ด๋‹น ์ด๋ฆ„์€ API์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.) @@ -1633,7 +1614,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -1643,8 +1624,8 @@ table_handle: An open smalltable.Table instance. keys: A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. - require_all_keys: Optional; If require_all_keys is True only - rows with values set for all keys will be returned. + require_all_keys: If True only rows with values set for all keys will be + returned. Returns: A dict mapping keys to the corresponding table row data @@ -1670,7 +1651,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -1683,8 +1664,7 @@ A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. require_all_keys: - Optional; If require_all_keys is True only rows with values set - for all keys will be returned. + If True only rows with values set for all keys will be returned. Returns: A dict mapping keys to the corresponding table row data @@ -1830,7 +1810,7 @@ if i & (i-1) == 0: # True if i is 0 or a power of 2. ``` - ํ•˜๋‚˜์˜ ํŒŒ์ผ์—๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `'` ๋˜๋Š” `"` ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ทธ๊ฒƒ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ๋‹ค๋งŒ `\\` ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. `gpylint` ๊ฐ€ ์ด๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. +- ๋‹ค๋งŒ backslash-escape ๋”ฐ์Œํ‘œ ๋ฌธ์ž ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. - ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ @@ -2155,15 +2135,19 @@ if i & (i-1) == 0: # True if i is 0 or a power of 2. --- + ### 3.15 ์ ‘๊ทผ ์ œ์–ด -- ๋งŒ์•ฝ ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ ๋ฌด์‹œํ•  ๊ฒฝ์šฐ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ถ”๊ฐ€์ ์ธ ๋น„์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜ ๋Œ€์‹ ์— public ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด `property`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ๋ฒ•์„ ์ผ๊ด€์ ์œผ๋กœ ์œ ์ง€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - -- ๋ฐ˜๋ฉด์— ์ ‘๊ทผ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋น„์šฉ์ด ํฐ ๊ฒฝ์šฐ, `get_foo()` ์™€ `set_foo()`์™€ ๊ฐ™์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ([๋„ค์ด๋ฐ](#s3.16-naming) ๊ฐ€์ด๋“œ ๋ผ์ธ์„ ์ฐธ๊ณ ํ•˜๋ผ)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋งŒ์•ฝ ์ „์— ํ–ˆ๋˜ ํ–‰๋™์ด property๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์„ ํ—ˆ๋ฝํ–ˆ๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ property์™€ ๋ฌถ์ง€๋งˆ์„ธ์š”. -- ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ์—ฌ์ „ํžˆ ๋ณ€์ˆ˜์— ์˜ค๋ž˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋ ค ์‹œ๋„ํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ๋ˆˆ์— ๋ณด์ด๊ฒŒ ๋ถ€์ˆ˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- Getter/Setter ํ•จ์ˆ˜(์ ‘๊ทผ์ž ๋ฐ ๋ณ€๊ฒฝ์ž๋ผ๊ณ ๋„ํ•จ)๋Š” ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์˜๋ฏธ ์žˆ๋Š” ์—ญํ• ์ด๋‚˜ ๋™์ž‘์„ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํŠนํžˆ ํ˜„์žฌ ๋˜๋Š” ํ•ฉ๋ฆฌ์ ์ธ ๋ฏธ๋ž˜์— ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋น„์šฉ์ด ์ƒ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ ์Œ์˜ getter/setter๊ฐ€ ๋‹จ์ˆœํžˆ ๋‚ด๋ถ€ ์†์„ฑ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฒฝ์šฐ ๋‚ด๋ถ€ ์†์„ฑ์€ ๋Œ€์‹  ๊ณต๊ฐœ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์— ๋น„ํ•ด ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ผ๋ถ€ ์ƒํƒœ๊ฐ€ ๋ฌดํšจํ™”๋˜๊ฑฐ๋‚˜ ๋‹ค์‹œ ์ž‘์„ฑ๋จ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋Š” setter ํ•จ์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•จ์ˆ˜ ํ˜ธ์ถœ์€ ์ž ์žฌ์ ์œผ๋กœ ์‚ฌ์†Œํ•˜์ง€ ์•Š์€ ์ž‘์—…์ด ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์Œ์„ ์•”์‹œํ•ฉ๋‹ˆ๋‹ค. +- ๋˜ํ•œ ๊ฐ„๋‹จํ•œ ๋…ผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๋” ์ด์ƒ getter/setter๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋„๋ก ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ [properties](#properties) ์˜ต์…˜์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Getter/Setter๋Š” `get_foo()`, `set_foo()`์™€ ๊ฐ™์€ [Naming](#s3.16-naming) ์ง€์นจ์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์ „ ๋™์ž‘์—์„œ property์„ ํ†ตํ•œ ์—‘์„ธ์Šค๊ฐ€ ํ—ˆ์šฉ๋œ ๊ฒฝ์šฐ ์ƒˆ getter/setterํ•จ์ˆ˜๋ฅผ property์— ๋ฐ”์ธ๋”ฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์—ฌ์ „ํžˆ ์ด์ „ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋ˆˆ์— ๋„๊ฒŒ ์ค‘๋‹จ๋˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. --- @@ -2425,13 +2409,15 @@ if __name__ == '__main__': #### 3.19.3 ์ „๋ฐฉ์„ ์–ธ -- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) `from __future__ import annotations`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๊ฐ„๋‹จํ•œ ๊ฒฝ์šฐ์—๋Š” ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํด๋ž˜์Šค ์ด๋ฆ„์— ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ```python +from __future__ import annotations + class MyClass: - def __init__(self, - stack: List["MyClass"]) -> None: + def __init__(self, stack: Sequence[MyClass]) -> None: ``` @@ -2583,54 +2569,41 @@ c = (1, "2", 3.5) # type: Tuple[int, str, float] - String ์ฃผ์„์— ๋Œ€ํ•œ ์ ์ ˆํ•œ Type์€ ์ฝ”๋“œ์˜ ์šฉ๋„์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. -- Python 3 ํ˜ธํ™˜ ์ฝ”๋“œ์ผ ๊ฒฝ์šฐ `str`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - `Text`๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. - - ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - -- Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - ๋“œ๋ฏ„ ๊ฒฝ์šฐ์— `str`์€ ์ ์ ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - - ์ผ๋ฐ˜์ ์œผ๋กœ ๋‘ Python ๋ฒ„์ „ ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•์Šต๋‹ˆ๋‹ค. Python 3์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” `unicode`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. - -- ์ด๋Ÿฐ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์€ Python ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. +- `str`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜์ง€๋งŒ `Text`๋„ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ(Python 2์—์„œ๋Š” `str` ๋˜๋Š” `unicode`, Python3์—์„œ๋Š” `str`)๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ๋ถ€์ ์ ˆํ•œ ์˜ˆ - - ```python - def py2_code(x: str) -> unicode: +```python +def deals_with_text_data_in_py3(x: str) -> str: ... - ``` - -- ์ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - ```python - def deals_with_binary_data(x: bytes) -> bytes: +def deals_with_binary_data(x: bytes) -> bytes: ... - ``` +def py2_compatible_text_data_processor(x: Text) -> Text: + ... +``` -- Python 2์—์„œ Text ๋ฐ์ดํ„ฐ(`str`, `unicode`๋Š” python 2, `str`์€ python 3)๋Š” `Text`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. -- Python 3์—์„œ๋งŒ Text ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ `str`๋ฅผ ์„ ํƒํ•˜์„ธ์š”. +- ์ผ๋ถ€ ํ”ํ•˜์ง€ ์•Š์€ Python2 ํ˜ธํ™˜์„ฑ ์‚ฌ๋ก€์—์„œ๋Š” `Text` ๋Œ€์‹  `str`์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ Python2์™€ Python3 ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- Python3์—๋Š” `unicode`๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์ด๋Ÿฌํ•œ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์ด Python3์™€ Python2์—์„œ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. - ```python - from typing import Text - ... - def py2_compatible(x: Text) -> Text: - ... - def py3_only(x: str) -> str: +- ๋ถ€์ ์ ˆํ•œ ์˜ˆ + +```python +def py2_code(x: str) -> unicode: ... - ``` +``` -- Type์ด byte ๋˜๋Š” Text ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ ์ ˆํ•œ Text Type๊ณผ ํ•จ๊ป˜ `Union`์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +If the type can be either bytes or text, use `Union`, with the appropriate text +type. - ```python - from typing import Text, Union - ... - def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: +```python +from typing import Text, Union +... +def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: ... - def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: +def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: ... - ``` +``` - ํ•จ์ˆ˜์˜ ๋ชจ๋“  string Type์ด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฝ์šฐ(์˜ˆ, ๋ฐ˜ํ™˜ Type์ด ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ธ์ž Type๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ) [AnyStr](#s3.19.10-type-var)๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.13 Properties.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.13 Properties.md" index 9664837..c525770 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.13 Properties.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.13 Properties.md" @@ -2,77 +2,41 @@ ### 2.13 Properties -- `Properties`์„ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ๋ณดํ†ต ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ธ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž๋‚˜ `setter` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- `Properties`๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์ด๋‚˜ ๋กœ์ง์ด ํ•„์š”ํ•œ attribute์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `Properties` ๊ตฌํ˜„์€ ๋ณดํ†ต์˜ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ธฐ๋Œ€์™€ ์ผ์น˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. #### 2.13.1 ์ •์˜ -- ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ํ•  ๋•Œ ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. #### 2.13.2 ์žฅ์  -- ๊ฐ„๋‹จํ•œ ์†์„ฑ ์ ‘๊ทผ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์ธ `get`, `set` ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๊ฐ€๋…์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +- getter, setter method ํ˜ธ์ถœ ๋Œ€์‹  attribute ์ ‘๊ทผ ๋ฐ ํ• ๋‹น API๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. +- attribute๋ฅผ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - [๋Š๊ธ‹ํ•œ ๊ณ„์‚ฐ๋ฒ•](https://ko.wikipedia.org/wiki/๋Š๊ธ‹ํ•œ_๊ณ„์‚ฐ๋ฒ•)์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. -- ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ [Pythonic](https://github.com/Yosseulsin-JOB/Google-Python-Style-Guide-kor/wiki/2.13-properties#pythonic)์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. -- ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ, trivial ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋Š” ์ง์ ‘ ๋ณ€์ˆ˜ ์ ‘๊ทผ์ด ํ•ฉ๋ฆฌ์ ์ผ ๋•Œ ์†์„ฑ ์šฐํšŒ๋ฅผ ํ—ˆ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํŒŒ๊ดดํ•˜์ง€ ์•Š๊ณ  ๋ฏธ๋ž˜์— ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. +- ๋‚ด๋ถ€๊ฐ€ class ์‚ฌ์šฉ์ž์™€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ฐœ์ „ํ•  ๋•Œ ํด๋ ˆ์Šค์˜ public interface๋ฅผ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. #### 2.13.3 ๋‹จ์  -- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.13.4 ๊ฒฐ๋ก  -- ์ƒˆ ์ฝ”๋“œ์—์„œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž ๋˜๋Š” `setter` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ์€ `@property` [decorator](#s2.17-function-and-method-decorators)๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ ์ž์ฒด๊ฐ€ ์žฌ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์†์„ฑ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋„๋กํ•˜๋ ค๋ฉด ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค([template method design pattern](https://en.wikipedia.org/wiki/Template_method_pattern)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.). -- ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ - - ```python - import math - class Square: - """๋‘ ๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์ง„ ์‚ฌ๊ฐํ˜•: ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋ฉด์ (area)๊ณผ ์ฝ๊ธฐ์ „์šฉ์ธ ๋‘˜๋ ˆ(perimeter) - - ์‚ฌ์šฉ๋ฐฉ๋ฒ•: - >>> sq = Square(3) - >>> sq.area - 9 - >>> sq.perimeter - 12 - >>> sq.area = 16 - >>> sq.side - 4 - >>> sq.perimeter - 16 - """ - - def __init__(self, side: float): - self.side = side - - @property - def area(self) -> float: - """์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.""" - return self._get_area() - - @area.setter - def area(self, area: float): - self._set_area(area) - - def _get_area(self) -> float: - """'๋ฉด์ '์†์„ฑ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์ ‘๊ทผ์ž์ž…๋‹ˆ๋‹ค.""" - return self.side ** 2 - - def _set_area(self, area: float): - """'๋ฉด์ ' ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์„ค์ •์ž์ž…๋‹ˆ๋‹ค.""" - self.side = math.sqrt(area) - - @property - def perimeter(self) -> float: - return self.side * 4 - ``` +- Properties๋Š” ํ—ˆ์šฉํ•˜์ง€๋งŒ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์ธ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ๊ธฐ๋Œ€์™€ ์ผ์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค + - ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” [getters and setters](#getters-and-setters)๊ทœ์น™์„ ๋”ฐ๋ฅด์„ธ์š”. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹จ์ˆœํžˆ attribute๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + - ๊ณ„์‚ฐ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ property๋Š” ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ([๋Œ€์‹  attribute๋ฅผ public์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.](#getters-and-setters)) +- ์ด์— ๋น„ํ•ด attribute ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ์‚ฌ์†Œํ•œ ํŒŒ์ƒ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. + - ๋กœ์ง์€ ๊ฐ„๋‹จํ•˜๊ณ  ๋†€๋ž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- Properties๋Š” `@property` [decorator](#s2.17-function-and-method-decorators)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- property descriptor๋ฅผ ์ˆ˜๋™์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ [power feature](#power-features)๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. +- ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. subclass๊ฐ€ ์žฌ์ •์˜ํ•˜๊ณ  ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ณ„์‚ฐ ๋„๊ตฌ๋กœ properties๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.14 True, False \355\217\211\352\260\200.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.14 True, False \355\217\211\352\260\200.md" index cbd7080..6679926 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.14 True, False \355\217\211\352\260\200.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.14 True, False \355\217\211\352\260\200.md" @@ -42,9 +42,6 @@ if not users: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo == 0: - self.handle_zero() - if i % 10 == 0: self.handle_multiple_of_ten() @@ -59,9 +56,6 @@ if len(users) == 0: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo is not None and not foo: - self.handle_zero() - if not i % 10: self.handle_multiple_of_ten() @@ -70,3 +64,4 @@ ``` - `'0'`(์ฆ‰, `0` ๋ฌธ์ž์—ด)์€ ์ฐธ์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- Numpy ๋ฐฐ์—ด์€ ์•”์‹œ์  boolean ์ปจํ…์ŠคํŠธ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `np.array`์˜ ๋น„์–ด ์žˆ์Œ์„ ํ…Œ์ŠคํŠธํ•  ๋•Œ `.size` attribute๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. (e.g. `if not users.size`) diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.17 \355\225\250\354\210\230\354\231\200 \353\251\224\354\204\234\353\223\234 Decorators.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.17 \355\225\250\354\210\230\354\231\200 \353\251\224\354\204\234\353\223\234 Decorators.md" index dbdd0c2..1dd350f 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.17 \355\225\250\354\210\230\354\231\200 \353\251\224\354\204\234\353\223\234 Decorators.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.17 \355\225\250\354\210\230\354\231\200 \353\251\224\354\204\234\353\223\234 Decorators.md" @@ -39,7 +39,9 @@ #### 2.17.3 ๋‹จ์  -- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” importํ•  ๋•Œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ Decorator ์ฝ”๋“œ๋Š” ํšŒ๋ณต์ด ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” object ์ •์˜ ์‹œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. module-level ๊ฐ์ฒด(classes, module functions, ...)์˜ ๊ฒฝ์šฐ importํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. +- Decorator ์ฝ”๋“œ ์˜ค๋ฅ˜๋Š” ๋ณต๊ตฌ๊ฐ€ ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.2 import.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.2 import.md" index 867d44b..540bedb 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.2 import.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.2 import.md" @@ -2,11 +2,7 @@ ### 2.2 Imports -- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์„ ๋Œ€์ƒ์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ณ  ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ - [typing ๋ชจ๋“ˆ](#s3.19.12-imports), - [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions), - ๊ทธ๋ฆฌ๊ณ  [six.moves module](https://six.readthedocs.io/#module-six.moves) - ์„ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค. +- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์— ์‚ฌ์šฉํ•˜๊ณ  ๊ฐœ๋ณ„ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ [typing ๋ชจ๋“ˆ](#s3.19.12-imports), [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions)์—์„œ ๊ฐ€์ ธ์˜จ ํด๋ž˜์Šค ๋ฐ [six.moves module](https://six.readthedocs.io/#module-six.moves)์—์„œ์˜ ๋ฆฌ๋””๋ ‰์…˜์€ ์ด ๊ทœ์น™์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค. @@ -45,4 +41,4 @@ - import๋œ๊ฒƒ๋“ค๊ณผ ๊ด€๋ จ์žˆ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. - ๋ชจ๋“ˆ์ด ๊ฐ™์€ ํŒจํ‚ค์ง€์— ์žˆ๋”๋ผ๋„ ์ „์ฒด ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. \ No newline at end of file +- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.20 \355\230\204\353\214\200\354\235\230 \355\214\214\354\235\264\354\215\254.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.20 \355\230\204\353\214\200\354\235\230 \355\214\214\354\235\264\354\215\254.md" index 474b872..26c355b 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.20 \355\230\204\353\214\200\354\235\230 \355\214\214\354\235\264\354\215\254.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.20 \355\230\204\353\214\200\354\235\230 \355\214\214\354\235\264\354\215\254.md" @@ -1,27 +1,29 @@ -### 2.20 Modern Python : Python 3 ๊ทธ๋ฆฌ๊ณ  from, \_\_future\_\_, imports +### 2.20 Modern Python : from, \_\_future\_\_, imports -- Python 3 ๋ฒ„์ „์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค! ์•„์ง ํ”„๋กœ์ ํŠธ์— Python 3์„ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ๋Š” ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๋ชจ๋“  ์ฝ”๋“œ๋Š” ํ˜ธํ™˜๋˜๋„๋ก ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์— Python 3์— ๋”ฐ๋ผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.) +- ์ƒˆ๋กœ์šด ์–ธ์–ด ๋ฒ„์ „ ์˜๋ฏธ ์ฒด๊ณ„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ์ด์ „ ๋Ÿฐํƒ€์ž„ ๋‚ด์—์„œ ํŒŒ์ผ ๋‹จ์œ„๋กœ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ํ–ฅํ›„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋’ค์— ์ œ์–ด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + (New language version semantic changes may be gated behind a special future import to enable them on a per-file basis within earlier runtimes.) #### 2.20.1 ์ •์˜ -- Python 3๋Š” Python์–ธ์–ด์—์„œ ์ค‘์š”ํ•œ ๋ณ€ํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ๋Š” 2.7 ๋ฒ„์ „์„ ์—ผ๋‘ํ•˜์—ฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. -- Python3์—์„œ ์ˆ˜์ •์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž˜ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝ”๋“œ์˜ ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ช‡๋ช‡ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +- `from __future__ import`๋ฌธ์„ ํ†ตํ•ด ๋ณด๋‹ค ํ˜„๋Œ€์ ์ธ ๊ธฐ๋Šฅ ์ค‘ ์ผ๋ถ€๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์˜ˆ์ƒ๋˜๋Š” ํ–ฅํ›„ Python ๋ฒ„์ „์˜ ๊ธฐ๋Šฅ์„ ์กฐ๊ธฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.20.2 ์žฅ์  -- Python 3๋ฅผ ์—ผ๋‘ํ•ด ๋‘๊ณ  ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋Š” ๋ช…ํ™•ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์˜ ๋ชจ๋“  ์˜์กด์„ฑ์ด Python 3์—์„œ ์‹คํ–‰ํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. +- ์ด๋Š” ํ˜ธํ™˜์„ฑ์„ ์„ ์–ธํ•˜๊ณ  ํ•ด๋‹น ํŒŒ์ผ ๋‚ด์—์„œ ํšŒ๊ท€๋ฅผ ๋ฐฉ์ง€ํ•˜๋ฉด์„œ ํŒŒ์ผ๋ณ„๋กœ ๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋Ÿฐํƒ€์ž„ ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ๋” ์›ํ™œํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ ์ž…์ค‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- ์ตœ์‹  ์ฝ”๋“œ๋Š” ํ–ฅํ›„ ๋Ÿฐํƒ€์ž„ ์—…๊ทธ๋ ˆ์ด๋“œ ์ค‘์— ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์  ๋ถ€์ฑ„๊ฐ€ ์ถ•์ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค. #### 2.20.3 ๋‹จ์  -- ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์ถ”๊ฐ€๋œ boilerplate๊ฐ€ ์ถ”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ์„ importํ•˜๋Š” ๊ฒƒ์€ ์ด๋ก€์ ์ž…๋‹ˆ๋‹ค. +- ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ feature ๋ฌธ์„ ๋„์ž…ํ•˜๊ธฐ ์ „์—๋Š” ๋งค์šฐ ์˜ค๋ž˜๋œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. @@ -29,7 +31,16 @@ ##### from \_\_future\_\_ imports -- `from __future__ import` ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋Š” ๋‹ค์Œ ์‚ฌํ•ญ์ด ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋ฉฐ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ํ˜ธํ™˜๋˜๋„๋ก ์—…๋ฐ์ดํŠธ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +- `from __future__ import`๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ฃผ์–ด์ง„ ์†Œ์ŠคํŒŒ์ผ์—์„œ ๋”์šฑ ํ˜„๋Œ€์ ์ธ Python ๊ตฌ๋ฌธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `__future__` import ๋’ค์— ๊ธฐ๋Šฅ์ด ์ˆจ๊ฒจ์ ธ ์žˆ๋Š” ๋ฒ„์ „์—์„œ ๋” ์ด์ƒ ์‹คํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ค„์„ ์ž์œ ๋กญ๊ฒŒ ์ œ๊ฑฐํ•˜์„ธ์š”. +- 3.7 ์ด์ƒ์ด ์•„๋‹Œ 3.5 ์ด์ „ ๋ฒ„์ „์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์—์„œ ๊ฐ€์ ธ์˜ฌ ๊ฒฝ์šฐ + + ```python + from __future__ import generator_stop + ``` + +- 2.7 ๋ฒ„์ „์„ ๊ณ„์† ์ง€์›ํ•ด์•ผํ•˜๋Š” ๋ถ€๋‹ด์ด ์žˆ๋Š” ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ ```python from __future__ import absolute_import @@ -37,10 +48,14 @@ from __future__ import print_function ``` -- `import`์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [absolute imports](https://www.python.org/dev/peps/pep-0328/), [`/` division behavior](https://www.python.org/dev/peps/pep-0238/), [the `print` function](https://www.python.org/dev/peps/pep-3105/)์„ ์ฐธ์กฐํ•˜์„ธ์š”. -- ์ด๋Ÿฌํ•œ import๋Š” ํ˜„์žฌ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. ๋ชจ๋“  ํŒŒ์ผ์— ํ•ญ์ƒ ํ–ฅํ›„ import๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‚˜์ค‘์— ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ํŽธ์ง‘ํ•˜๋Š” ๋™์•ˆ ์žŠ์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- ๋‹ค๋ฅธ `from __future__` import ๋ช…์„ธ๋„ ์žˆ์œผ๋‹ˆ ์•Œ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `unicode_literals`๋Š” ํŒŒ์ด์ฌ 2.7 ๋‚ด ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ๋„์ž…๋˜๋Š” ์•”๋ฌต์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ ๋•Œ๋ฌธ์— ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ๊ณ ์‚ฌํ•ญ์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋Š” ํ•„์š”์— ๋”ฐ๋ผ `b''`, `u''` ๋ฐ”์ดํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด literal๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Python future statement definitions](https://docs.python.org/3/library/__future__.html) ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด์„ธ์š”. +- ์ฝ”๋“œ๊ฐ€ ์ถฉ๋ถ„ํžˆ ํ˜„๋Œ€์ ์ธ ํ™˜๊ฒฝ์—์„œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ํ™•์‹ ์ด ๋“ค ๋•Œ๊นŒ์ง€ ์ด๋Ÿฌํ•œ import๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. +- ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ํŠน์ • ํ–ฅํ›„ import๋ฅผ ํ†ตํ•ด ํ™œ์„ฑํ™”๋˜๋Š” ๊ธฐ๋Šฅ์„ ํ˜„์žฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ํŒŒ์ผ์— ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์œ ์ง€ํ•˜๋ฉด ๋‚˜์ค‘์— ์ฝ”๋“œ๊ฐ€ ์ด์ „ ๋™์ž‘์— ๋”ฐ๋ผ ์‹ค์ˆ˜๋กœ ์ˆ˜์ •๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋‹ค๋ฅธ `from __future__` import ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- 2.7๋ฒ„์ „ ๋‚ด์— ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ๋„์ž…๋œ ์•”์‹œ์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ๋กœ ์ธํ•ด ํ™•์‹คํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— 2.7๋ฒ„์ „์— ๋Œ€ํ•œ ๊ถŒ์žฅ ์‚ฌํ•ญ์— `unicode_literals`๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. +- ๋Œ€๋ถ€๋ถ„์˜ ์ด์ค‘ ๋ฒ„์ „ 2-3 ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ `b''` ์™€ `u''` q๋ฐ”ํŠธ์™€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์•˜์Šต๋‹ˆ๋‹ค. ##### six, future ๊ทธ๋ฆฌ๊ณ  past ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ -- ํ”„๋กœ์ ํŠธ๊ฐ€ Python 2, 3 ๋ชจ๋‘ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ ํ•ฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ์ ํŠธ๊ฐ€ ์—ฌ์ „ํžˆ Python 2, 3 ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ ํ•ฉํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š” ๋Œ€๋กœ [six](https://pypi.org/project/six/), [future](https://pypi.org/project/future/), ๋ฐ [past](https://pypi.org/project/past/) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.3 package.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.3 package.md" index 4ccf263..844e3bc 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.3 package.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.3 package.md" @@ -42,7 +42,7 @@ FLAGS = flags.FLAGS ``` - - ๋ถ€์ ์ ˆํ•œ ์˜ˆ (์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.) + - ๋ถ€์ ์ ˆํ•œ ์˜ˆ _(์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.)_ ```python # Unclear what module the author wanted and what will be imported. The actual diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.4 \354\230\210\354\231\270\354\262\230\353\246\254.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.4 \354\230\210\354\231\270\354\262\230\353\246\254.md" index 9ba0890..48dbd10 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.4 \354\230\210\354\231\270\354\262\230\353\246\254.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/2.4 \354\230\210\354\231\270\354\262\230\353\246\254.md" @@ -79,7 +79,7 @@ - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํŒจํ‚ค์ง€๋Š” ๊ณ ์œ ์˜ ์˜ˆ์™ธ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์‚ฌ์šฉํ•˜๋Š” ๋™์•ˆ, ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค(exception class)๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋ง๋”๋“ฌ์ด(stutter)๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). +- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋˜ํ’€์ด๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). - ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ์“ฐ๋ ˆ๋“œ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ชฝ ๋ธ”๋ก์— ์žˆ์ง€์•Š์œผ๋ฉด ์ ˆ๋Œ€ ํฌ๊ด„์ ์ธ `except:`๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ `Exception`, `StandardError`์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. (๊ทธ๋ฆฌ๊ณ  ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.) Python์€ ์ด์™€ ๊ด€๋ จํ•ด์„œ ๋งค์šฐ ๊ด€์šฉ์ ์ด๋ฉฐ `except:` ๋ชจ๋“  ์˜คํƒˆ์ž๋ฅผ ๋น„๋กฏํ•˜์—ฌ, sys.exit() ํ˜ธ์ถœ, Ctrl+C๋กœ ์ธํ•œ ์ธํ„ฐ๋ŸฝํŠธ, ์œ ๋‹›ํ…Œ์ŠคํŠธ ์‹คํŒจ์™€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹น์‹ ์ด ํฌ์ฐฉ์„ ์›ํ•˜์ง€ ์•Š์•˜๋˜ ๋‹ค๋ฅธ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋“ค๊นŒ์ง€ ๋ชจ๋‘ ์žก์•„๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์ฝ”๋“œ์ƒ์—์„œ `try`/`except` ๋ธ”๋ก์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™”์‹œํ‚ค์„ธ์š”. `try`๋ฌธ์˜ ๋‚ด๋ถ€๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์˜ˆ์™ธ๋Š” ๋‹น์‹ ์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜์ง€ ์•Š์•˜๋˜ ์ฝ”๋“œ์— ์˜ํ•ด ์ ์  ๋” ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, `try`/`except` ๋ธ”๋ก์€ ์ง„์งœ ๊ฒ€์ถœํ•ด์•ผ ํ•  ์—๋Ÿฌ๋ฅผ ๊ฐ€๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. - ์˜ˆ์™ธ๊ฐ€ `try` ๋ธ”๋ก์—์„œ ๋ฐœ์ƒํ•˜๋˜ ์•ˆํ•˜๋˜ `finally`์ ˆ์€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์ด๊ฑด ๊ฐ€๋” ๊น”๋”ํžˆ ํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ํŒŒ์ผ์„ ๋‹ซ์„ ๋•Œ ๊ฐ€ ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/ReadMe.md" "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/ReadMe.md" index 7d80fa8..cedd157 100644 --- "a/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/ReadMe.md" +++ "b/Google Python Style Guide/2. Python \354\226\270\354\226\264 \352\267\234\354\271\231/ReadMe.md" @@ -74,11 +74,7 @@ ### 2.2 Imports -- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์„ ๋Œ€์ƒ์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ณ  ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ - [typing ๋ชจ๋“ˆ](#s3.19.12-imports), - [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions), - ๊ทธ๋ฆฌ๊ณ  [six.moves module](https://six.readthedocs.io/#module-six.moves) - ์„ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค. +- `import`๋ฌธ์„ ์‚ฌ์šฉํ• ๋•Œ package์™€ module์— ์‚ฌ์šฉํ•˜๊ณ  ๊ฐœ๋ณ„ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ [typing ๋ชจ๋“ˆ](#s3.19.12-imports), [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions)์—์„œ ๊ฐ€์ ธ์˜จ ํด๋ž˜์Šค ๋ฐ [six.moves module](https://six.readthedocs.io/#module-six.moves)์—์„œ์˜ ๋ฆฌ๋””๋ ‰์…˜์€ ์ด ๊ทœ์น™์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค. @@ -117,7 +113,8 @@ - import๋œ๊ฒƒ๋“ค๊ณผ ๊ด€๋ จ์žˆ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. - ๋ชจ๋“ˆ์ด ๊ฐ™์€ ํŒจํ‚ค์ง€์— ์žˆ๋”๋ผ๋„ ์ „์ฒด ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. +- ์ด๋Š” ๋ฌด์‹ฌ์ฝ” ํŒจํ‚ค์ง€๋ฅผ ๋‘๋ฒˆ import ํ•˜๋Š”๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๋Š” ๊ฒƒ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. + --- @@ -163,7 +160,7 @@ FLAGS = flags.FLAGS ``` - - ๋ถ€์ ์ ˆํ•œ ์˜ˆ (์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.) + - ๋ถ€์ ์ ˆํ•œ ์˜ˆ _(์ด ํŒŒ์ผ์€ `doctor/who/` ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  `jodie.py`๋˜ํ•œ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.)_ ```python # Unclear what module the author wanted and what will be imported. The actual @@ -257,7 +254,7 @@ - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํŒจํ‚ค์ง€๋Š” ๊ณ ์œ ์˜ ์˜ˆ์™ธ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์‚ฌ์šฉํ•˜๋Š” ๋™์•ˆ, ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค(exception class)๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋ง๋”๋“ฌ์ด(stutter)๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). +- ์˜ˆ์™ธ ์ด๋ฆ„์€ `Error`๋กœ ๋๋‚˜์•ผ ํ•˜๊ณ  ๋˜ํ’€์ด๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(`foo.fooError`). - ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ์“ฐ๋ ˆ๋“œ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ชฝ ๋ธ”๋ก์— ์žˆ์ง€์•Š์œผ๋ฉด ์ ˆ๋Œ€ ํฌ๊ด„์ ์ธ `except:`๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ `Exception`, `StandardError`์„ ์‚ฌ์šฉํ•˜์ง€๋งˆ์„ธ์š”. (๊ทธ๋ฆฌ๊ณ  ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.) Python์€ ์ด์™€ ๊ด€๋ จํ•ด์„œ ๋งค์šฐ ๊ด€์šฉ์ ์ด๋ฉฐ `except:` ๋ชจ๋“  ์˜คํƒˆ์ž๋ฅผ ๋น„๋กฏํ•˜์—ฌ, sys.exit() ํ˜ธ์ถœ, Ctrl+C๋กœ ์ธํ•œ ์ธํ„ฐ๋ŸฝํŠธ, ์œ ๋‹›ํ…Œ์ŠคํŠธ ์‹คํŒจ์™€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹น์‹ ์ด ํฌ์ฐฉ์„ ์›ํ•˜์ง€ ์•Š์•˜๋˜ ๋‹ค๋ฅธ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋“ค๊นŒ์ง€ ๋ชจ๋‘ ์žก์•„๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. - ์ฝ”๋“œ์ƒ์—์„œ `try`/`except` ๋ธ”๋ก์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™”์‹œํ‚ค์„ธ์š”. `try`๋ฌธ์˜ ๋‚ด๋ถ€๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์˜ˆ์™ธ๋Š” ๋‹น์‹ ์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜์ง€ ์•Š์•˜๋˜ ์ฝ”๋“œ์— ์˜ํ•ด ์ ์  ๋” ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, `try`/`except` ๋ธ”๋ก์€ ์ง„์งœ ๊ฒ€์ถœํ•ด์•ผ ํ•  ์—๋Ÿฌ๋ฅผ ๊ฐ€๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. - ์˜ˆ์™ธ๊ฐ€ `try` ๋ธ”๋ก์—์„œ ๋ฐœ์ƒํ•˜๋˜ ์•ˆํ•˜๋˜ `finally`์ ˆ์€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์ด๊ฑด ๊ฐ€๋” ๊น”๋”ํžˆ ํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ํŒŒ์ผ์„ ๋‹ซ์„ ๋•Œ ๊ฐ€ ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค. @@ -677,80 +674,44 @@ ### 2.13 Properties -- `Properties`์„ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ๋ณดํ†ต ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ธ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž๋‚˜ `setter` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- `Properties`๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์ด๋‚˜ ๋กœ์ง์ด ํ•„์š”ํ•œ attribute์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `Properties` ๊ตฌํ˜„์€ ๋ณดํ†ต์˜ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ธฐ๋Œ€์™€ ์ผ์น˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. #### 2.13.1 ์ •์˜ -- ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ํ•  ๋•Œ ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์„ ์ ‘๊ทผํ•˜๋“ฏ์ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํฌ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. #### 2.13.2 ์žฅ์  -- ๊ฐ„๋‹จํ•œ ์†์„ฑ ์ ‘๊ทผ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์ธ `get`, `set` ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๊ฐ€๋…์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +- getter, setter method ํ˜ธ์ถœ ๋Œ€์‹  attribute ์ ‘๊ทผ ๋ฐ ํ• ๋‹น API๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. +- attribute๋ฅผ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - [๋Š๊ธ‹ํ•œ ๊ณ„์‚ฐ๋ฒ•](https://ko.wikipedia.org/wiki/๋Š๊ธ‹ํ•œ_๊ณ„์‚ฐ๋ฒ•)์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. -- ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ [Pythonic](https://github.com/Yosseulsin-JOB/Google-Python-Style-Guide-kor/wiki/2.13-properties#pythonic)์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. -- ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ, trivial ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋Š” ์ง์ ‘ ๋ณ€์ˆ˜ ์ ‘๊ทผ์ด ํ•ฉ๋ฆฌ์ ์ผ ๋•Œ ์†์„ฑ ์šฐํšŒ๋ฅผ ํ—ˆ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํŒŒ๊ดดํ•˜์ง€ ์•Š๊ณ  ๋ฏธ๋ž˜์— ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. +- ๋‚ด๋ถ€๊ฐ€ class ์‚ฌ์šฉ์ž์™€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ฐœ์ „ํ•  ๋•Œ ํด๋ ˆ์Šค์˜ public interface๋ฅผ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. #### 2.13.3 ๋‹จ์  -- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„ ๋กœ๋”ฉ(operator overloading)๊ณผ ๊ฐ™์€ ๋ถ€์ž‘์šฉ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.13.4 ๊ฒฐ๋ก  -- ์ƒˆ ์ฝ”๋“œ์—์„œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋ฒผ์šด ์ ‘๊ทผ์ž ๋˜๋Š” `setter` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ์€ `@property` [decorator](#s2.17-function-and-method-decorators)๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์†์„ฑ ์ž์ฒด๊ฐ€ ์žฌ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์†์„ฑ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋„๋กํ•˜๋ ค๋ฉด ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค([template method design pattern](https://en.wikipedia.org/wiki/Template_method_pattern)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.). -- ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ - - ```python - import math - class Square: - """๋‘ ๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์ง„ ์‚ฌ๊ฐํ˜•: ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋ฉด์ (area)๊ณผ ์ฝ๊ธฐ์ „์šฉ์ธ ๋‘˜๋ ˆ(perimeter) - - ์‚ฌ์šฉ๋ฐฉ๋ฒ•: - >>> sq = Square(3) - >>> sq.area - 9 - >>> sq.perimeter - 12 - >>> sq.area = 16 - >>> sq.side - 4 - >>> sq.perimeter - 16 - """ - - def __init__(self, side: float): - self.side = side - - @property - def area(self) -> float: - """์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.""" - return self._get_area() - - @area.setter - def area(self, area: float): - self._set_area(area) - - def _get_area(self) -> float: - """'๋ฉด์ '์†์„ฑ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์ ‘๊ทผ์ž์ž…๋‹ˆ๋‹ค.""" - return self.side ** 2 - - def _set_area(self, area: float): - """'๋ฉด์ ' ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘ ์„ค์ •์ž์ž…๋‹ˆ๋‹ค.""" - self.side = math.sqrt(area) - - @property - def perimeter(self) -> float: - return self.side * 4 - ``` +- Properties๋Š” ํ—ˆ์šฉํ•˜์ง€๋งŒ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์ธ attribute ์ ‘๊ทผ์— ๋Œ€ํ•œ ๊ธฐ๋Œ€์™€ ์ผ์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค + - ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” [getters and setters](#getters-and-setters)๊ทœ์น™์„ ๋”ฐ๋ฅด์„ธ์š”. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹จ์ˆœํžˆ attribute๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + - ๊ณ„์‚ฐ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ property๋Š” ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ([๋Œ€์‹  attribute๋ฅผ public์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.](#getters-and-setters)) +- ์ด์— ๋น„ํ•ด attribute ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ์‚ฌ์†Œํ•œ ํŒŒ์ƒ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด property๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. + - ๋กœ์ง์€ ๊ฐ„๋‹จํ•˜๊ณ  ๋†€๋ž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- Properties๋Š” `@property` [decorator](#s2.17-function-and-method-decorators)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- property descriptor๋ฅผ ์ˆ˜๋™์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ [power feature](#power-features)๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. +- ์†์„ฑ์— ๋Œ€ํ•œ ์ƒ์†์€ ๋ช…๋ฐฑํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. subclass๊ฐ€ ์žฌ์ •์˜ํ•˜๊ณ  ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ณ„์‚ฐ ๋„๊ตฌ๋กœ properties๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. --- @@ -797,9 +758,6 @@ if not users: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo == 0: - self.handle_zero() - if i % 10 == 0: self.handle_multiple_of_ten() @@ -814,9 +772,6 @@ if len(users) == 0: print('์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.') - if foo is not None and not foo: - self.handle_zero() - if not i % 10: self.handle_multiple_of_ten() @@ -825,6 +780,7 @@ ``` - `'0'`(์ฆ‰, `0` ๋ฌธ์ž์—ด)์€ ์ฐธ์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. +- Numpy ๋ฐฐ์—ด์€ ์•”์‹œ์  boolean ์ปจํ…์ŠคํŠธ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `np.array`์˜ ๋น„์–ด ์žˆ์Œ์„ ํ…Œ์ŠคํŠธํ•  ๋•Œ `.size` attribute๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. (e.g. `if not users.size`) --- @@ -927,7 +883,9 @@ #### 2.17.3 ๋‹จ์  -- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” importํ•  ๋•Œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ Decorator ์ฝ”๋“œ๋Š” ํšŒ๋ณต์ด ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +- Decorator๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•ด ์ž„์˜์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋†€๋ผ์šด ์•”๋ฌต์  ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๊ฒŒ๋‹ค๊ฐ€, Decorator๋Š” object ์ •์˜ ์‹œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. module-level ๊ฐ์ฒด(classes, module functions, ...)์˜ ๊ฒฝ์šฐ importํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. +- Decorator ์ฝ”๋“œ ์˜ค๋ฅ˜๋Š” ๋ณต๊ตฌ๊ฐ€ ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. @@ -982,28 +940,30 @@ --- -### 2.20 Modern Python : Python 3 ๊ทธ๋ฆฌ๊ณ  from, \_\_future\_\_, imports +### 2.20 Modern Python : from, \_\_future\_\_, imports -- Python 3 ๋ฒ„์ „์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค! ์•„์ง ํ”„๋กœ์ ํŠธ์— Python 3์„ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ๋Š” ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ๋ชจ๋“  ์ฝ”๋“œ๋Š” ํ˜ธํ™˜๋˜๋„๋ก ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์— Python 3์— ๋”ฐ๋ผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.) +- ์ƒˆ๋กœ์šด ์–ธ์–ด ๋ฒ„์ „ ์˜๋ฏธ ์ฒด๊ณ„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ์ด์ „ ๋Ÿฐํƒ€์ž„ ๋‚ด์—์„œ ํŒŒ์ผ ๋‹จ์œ„๋กœ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ํ–ฅํ›„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋’ค์— ์ œ์–ด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + (New language version semantic changes may be gated behind a special future import to enable them on a per-file basis within earlier runtimes.) #### 2.20.1 ์ •์˜ -- Python 3๋Š” Python์–ธ์–ด์—์„œ ์ค‘์š”ํ•œ ๋ณ€ํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ๋Š” 2.7 ๋ฒ„์ „์„ ์—ผ๋‘ํ•˜์—ฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. -- Python3์—์„œ ์ˆ˜์ •์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž˜ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝ”๋“œ์˜ ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ช‡๋ช‡ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +- `from __future__ import`๋ฌธ์„ ํ†ตํ•ด ๋ณด๋‹ค ํ˜„๋Œ€์ ์ธ ๊ธฐ๋Šฅ ์ค‘ ์ผ๋ถ€๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์˜ˆ์ƒ๋˜๋Š” ํ–ฅํ›„ Python ๋ฒ„์ „์˜ ๊ธฐ๋Šฅ์„ ์กฐ๊ธฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #### 2.20.2 ์žฅ์  -- Python 3๋ฅผ ์—ผ๋‘ํ•ด ๋‘๊ณ  ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋Š” ๋ช…ํ™•ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์˜ ๋ชจ๋“  ์˜์กด์„ฑ์ด Python 3์—์„œ ์‹คํ–‰ํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. +- ์ด๋Š” ํ˜ธํ™˜์„ฑ์„ ์„ ์–ธํ•˜๊ณ  ํ•ด๋‹น ํŒŒ์ผ ๋‚ด์—์„œ ํšŒ๊ท€๋ฅผ ๋ฐฉ์ง€ํ•˜๋ฉด์„œ ํŒŒ์ผ๋ณ„๋กœ ๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋Ÿฐํƒ€์ž„ ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ๋” ์›ํ™œํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ ์ž…์ค‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- ์ตœ์‹  ์ฝ”๋“œ๋Š” ํ–ฅํ›„ ๋Ÿฐํƒ€์ž„ ์—…๊ทธ๋ ˆ์ด๋“œ ์ค‘์— ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์  ๋ถ€์ฑ„๊ฐ€ ์ถ•์ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค. #### 2.20.3 ๋‹จ์  -- ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์ถ”๊ฐ€๋œ boilerplate๊ฐ€ ์ถ”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ์„ importํ•˜๋Š” ๊ฒƒ์€ ์ด๋ก€์ ์ž…๋‹ˆ๋‹ค. +- ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ feature ๋ฌธ์„ ๋„์ž…ํ•˜๊ธฐ ์ „์—๋Š” ๋งค์šฐ ์˜ค๋ž˜๋œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. @@ -1011,7 +971,16 @@ ##### from \_\_future\_\_ imports -- `from __future__ import` ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋Š” ๋‹ค์Œ ์‚ฌํ•ญ์ด ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋ฉฐ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ํ˜ธํ™˜๋˜๋„๋ก ์—…๋ฐ์ดํŠธ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +- `from __future__ import`๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ฃผ์–ด์ง„ ์†Œ์ŠคํŒŒ์ผ์—์„œ ๋”์šฑ ํ˜„๋Œ€์ ์ธ Python ๊ตฌ๋ฌธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `__future__` import ๋’ค์— ๊ธฐ๋Šฅ์ด ์ˆจ๊ฒจ์ ธ ์žˆ๋Š” ๋ฒ„์ „์—์„œ ๋” ์ด์ƒ ์‹คํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ค„์„ ์ž์œ ๋กญ๊ฒŒ ์ œ๊ฑฐํ•˜์„ธ์š”. +- 3.7 ์ด์ƒ์ด ์•„๋‹Œ 3.5 ์ด์ „ ๋ฒ„์ „์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์—์„œ ๊ฐ€์ ธ์˜ฌ ๊ฒฝ์šฐ + + ```python + from __future__ import generator_stop + ``` + +- 2.7 ๋ฒ„์ „์„ ๊ณ„์† ์ง€์›ํ•ด์•ผํ•˜๋Š” ๋ถ€๋‹ด์ด ์žˆ๋Š” ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ ```python from __future__ import absolute_import @@ -1019,13 +988,17 @@ from __future__ import print_function ``` -- `import`์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [absolute imports](https://www.python.org/dev/peps/pep-0328/), [`/` division behavior](https://www.python.org/dev/peps/pep-0238/), [the `print` function](https://www.python.org/dev/peps/pep-3105/)์„ ์ฐธ์กฐํ•˜์„ธ์š”. -- ์ด๋Ÿฌํ•œ import๋Š” ํ˜„์žฌ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. ๋ชจ๋“  ํŒŒ์ผ์— ํ•ญ์ƒ ํ–ฅํ›„ import๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‚˜์ค‘์— ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ํŽธ์ง‘ํ•˜๋Š” ๋™์•ˆ ์žŠ์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- ๋‹ค๋ฅธ `from __future__` import ๋ช…์„ธ๋„ ์žˆ์œผ๋‹ˆ ์•Œ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `unicode_literals`๋Š” ํŒŒ์ด์ฌ 2.7 ๋‚ด ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ๋„์ž…๋˜๋Š” ์•”๋ฌต์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ ๋•Œ๋ฌธ์— ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ๊ณ ์‚ฌํ•ญ์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋Š” ํ•„์š”์— ๋”ฐ๋ผ `b''`, `u''` ๋ฐ”์ดํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด literal๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Python future statement definitions](https://docs.python.org/3/library/__future__.html) ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด์„ธ์š”. +- ์ฝ”๋“œ๊ฐ€ ์ถฉ๋ถ„ํžˆ ํ˜„๋Œ€์ ์ธ ํ™˜๊ฒฝ์—์„œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ํ™•์‹ ์ด ๋“ค ๋•Œ๊นŒ์ง€ ์ด๋Ÿฌํ•œ import๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. +- ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ํŠน์ • ํ–ฅํ›„ import๋ฅผ ํ†ตํ•ด ํ™œ์„ฑํ™”๋˜๋Š” ๊ธฐ๋Šฅ์„ ํ˜„์žฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ํŒŒ์ผ์— ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์œ ์ง€ํ•˜๋ฉด ๋‚˜์ค‘์— ์ฝ”๋“œ๊ฐ€ ์ด์ „ ๋™์ž‘์— ๋”ฐ๋ผ ์‹ค์ˆ˜๋กœ ์ˆ˜์ •๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋‹ค๋ฅธ `from __future__` import ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- 2.7๋ฒ„์ „ ๋‚ด์— ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ๋„์ž…๋œ ์•”์‹œ์  ๊ธฐ๋ณธ ์ฝ”๋ฑ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ๋กœ ์ธํ•ด ํ™•์‹คํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— 2.7๋ฒ„์ „์— ๋Œ€ํ•œ ๊ถŒ์žฅ ์‚ฌํ•ญ์— `unicode_literals`๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. +- ๋Œ€๋ถ€๋ถ„์˜ ์ด์ค‘ ๋ฒ„์ „ 2-3 ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ `b''` ์™€ `u''` q๋ฐ”ํŠธ์™€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์•˜์Šต๋‹ˆ๋‹ค. ##### six, future ๊ทธ๋ฆฌ๊ณ  past ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ -- ํ”„๋กœ์ ํŠธ๊ฐ€ Python 2, 3 ๋ชจ๋‘ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ ํ•ฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ์ ํŠธ๊ฐ€ ์—ฌ์ „ํžˆ Python 2, 3 ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ์„ ์ง€์›ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ ํ•ฉํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š” ๋Œ€๋กœ [six](https://pypi.org/project/six/), [future](https://pypi.org/project/future/), ๋ฐ [past](https://pypi.org/project/past/) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์ฝ”๋“œ๋ฅผ ๋” ๊นจ๋—ํ•˜๊ณ  ์‚ถ์„ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. --- diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.10 \353\254\270\354\236\220\354\227\264.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.10 \353\254\270\354\236\220\354\227\264.md" index bc9e1f9..b54023b 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.10 \353\254\270\354\236\220\354\227\264.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.10 \353\254\270\354\236\220\354\227\264.md" @@ -51,7 +51,7 @@ ``` - ํ•˜๋‚˜์˜ ํŒŒ์ผ์—๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `'` ๋˜๋Š” `"` ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ทธ๊ฒƒ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ๋‹ค๋งŒ `\\` ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. `gpylint` ๊ฐ€ ์ด๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. +- ๋‹ค๋งŒ backslash-escape ๋”ฐ์Œํ‘œ ๋ฌธ์ž ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. - ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.15 \354\240\221\352\267\274 \354\240\234\354\226\264.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.15 \354\240\221\352\267\274 \354\240\234\354\226\264.md" index 2020f94..bb0ffae 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.15 \354\240\221\352\267\274 \354\240\234\354\226\264.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.15 \354\240\221\352\267\274 \354\240\234\354\226\264.md" @@ -1,10 +1,14 @@ + ### 3.15 ์ ‘๊ทผ ์ œ์–ด -- ๋งŒ์•ฝ ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ ๋ฌด์‹œํ•  ๊ฒฝ์šฐ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ถ”๊ฐ€์ ์ธ ๋น„์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜ ๋Œ€์‹ ์— public ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด `property`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ๋ฒ•์„ ์ผ๊ด€์ ์œผ๋กœ ์œ ์ง€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - -- ๋ฐ˜๋ฉด์— ์ ‘๊ทผ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋น„์šฉ์ด ํฐ ๊ฒฝ์šฐ, `get_foo()` ์™€ `set_foo()`์™€ ๊ฐ™์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ([๋„ค์ด๋ฐ](#s3.16-naming) ๊ฐ€์ด๋“œ ๋ผ์ธ์„ ์ฐธ๊ณ ํ•˜๋ผ)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋งŒ์•ฝ ์ „์— ํ–ˆ๋˜ ํ–‰๋™์ด property๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์„ ํ—ˆ๋ฝํ–ˆ๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ property์™€ ๋ฌถ์ง€๋งˆ์„ธ์š”. -- ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ์—ฌ์ „ํžˆ ๋ณ€์ˆ˜์— ์˜ค๋ž˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋ ค ์‹œ๋„ํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ๋ˆˆ์— ๋ณด์ด๊ฒŒ ๋ถ€์ˆ˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- Getter/Setter ํ•จ์ˆ˜(์ ‘๊ทผ์ž ๋ฐ ๋ณ€๊ฒฝ์ž๋ผ๊ณ ๋„ํ•จ)๋Š” ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์˜๋ฏธ ์žˆ๋Š” ์—ญํ• ์ด๋‚˜ ๋™์ž‘์„ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํŠนํžˆ ํ˜„์žฌ ๋˜๋Š” ํ•ฉ๋ฆฌ์ ์ธ ๋ฏธ๋ž˜์— ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋น„์šฉ์ด ์ƒ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ ์Œ์˜ getter/setter๊ฐ€ ๋‹จ์ˆœํžˆ ๋‚ด๋ถ€ ์†์„ฑ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฒฝ์šฐ ๋‚ด๋ถ€ ์†์„ฑ์€ ๋Œ€์‹  ๊ณต๊ฐœ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์— ๋น„ํ•ด ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ผ๋ถ€ ์ƒํƒœ๊ฐ€ ๋ฌดํšจํ™”๋˜๊ฑฐ๋‚˜ ๋‹ค์‹œ ์ž‘์„ฑ๋จ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋Š” setter ํ•จ์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•จ์ˆ˜ ํ˜ธ์ถœ์€ ์ž ์žฌ์ ์œผ๋กœ ์‚ฌ์†Œํ•˜์ง€ ์•Š์€ ์ž‘์—…์ด ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์Œ์„ ์•”์‹œํ•ฉ๋‹ˆ๋‹ค. +- ๋˜ํ•œ ๊ฐ„๋‹จํ•œ ๋…ผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๋” ์ด์ƒ getter/setter๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋„๋ก ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ [properties](#properties) ์˜ต์…˜์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Getter/Setter๋Š” `get_foo()`, `set_foo()`์™€ ๊ฐ™์€ [Naming](#s3.16-naming) ์ง€์นจ์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์ „ ๋™์ž‘์—์„œ property์„ ํ†ตํ•œ ์—‘์„ธ์Šค๊ฐ€ ํ—ˆ์šฉ๋œ ๊ฒฝ์šฐ ์ƒˆ getter/setterํ•จ์ˆ˜๋ฅผ property์— ๋ฐ”์ธ๋”ฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์—ฌ์ „ํžˆ ์ด์ „ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋ˆˆ์— ๋„๊ฒŒ ์ค‘๋‹จ๋˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.19 Type \354\243\274\354\204\235 \353\260\251\353\262\225.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.19 Type \354\243\274\354\204\235 \353\260\251\353\262\225.md" index e4bf4a3..2502927 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.19 Type \354\243\274\354\204\235 \353\260\251\353\262\225.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.19 Type \354\243\274\354\204\235 \353\260\251\353\262\225.md" @@ -111,13 +111,15 @@ #### 3.19.3 ์ „๋ฐฉ์„ ์–ธ -- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) `from __future__ import annotations`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๊ฐ„๋‹จํ•œ ๊ฒฝ์šฐ์—๋Š” ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํด๋ž˜์Šค ์ด๋ฆ„์— ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ```python +from __future__ import annotations + class MyClass: - def __init__(self, - stack: List["MyClass"]) -> None: + def __init__(self, stack: Sequence[MyClass]) -> None: ``` @@ -269,54 +271,41 @@ c = (1, "2", 3.5) # type: Tuple[int, str, float] - String ์ฃผ์„์— ๋Œ€ํ•œ ์ ์ ˆํ•œ Type์€ ์ฝ”๋“œ์˜ ์šฉ๋„์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. -- Python 3 ํ˜ธํ™˜ ์ฝ”๋“œ์ผ ๊ฒฝ์šฐ `str`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - `Text`๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. - - ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - -- Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - ๋“œ๋ฏ„ ๊ฒฝ์šฐ์— `str`์€ ์ ์ ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - - ์ผ๋ฐ˜์ ์œผ๋กœ ๋‘ Python ๋ฒ„์ „ ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•์Šต๋‹ˆ๋‹ค. Python 3์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” `unicode`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- `str`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜์ง€๋งŒ `Text`๋„ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ(Python 2์—์„œ๋Š” `str` ๋˜๋Š” `unicode`, Python3์—์„œ๋Š” `str`)๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ์ด๋Ÿฐ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์€ Python ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. - -- ๋ถ€์ ์ ˆํ•œ ์˜ˆ - - ```python - def py2_code(x: str) -> unicode: +```python +def deals_with_text_data_in_py3(x: str) -> str: ... - ``` - -- ์ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - ```python - def deals_with_binary_data(x: bytes) -> bytes: +def deals_with_binary_data(x: bytes) -> bytes: ... - ``` +def py2_compatible_text_data_processor(x: Text) -> Text: + ... +``` -- Python 2์—์„œ Text ๋ฐ์ดํ„ฐ(`str`, `unicode`๋Š” python 2, `str`์€ python 3)๋Š” `Text`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. -- Python 3์—์„œ๋งŒ Text ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ `str`๋ฅผ ์„ ํƒํ•˜์„ธ์š”. +- ์ผ๋ถ€ ํ”ํ•˜์ง€ ์•Š์€ Python2 ํ˜ธํ™˜์„ฑ ์‚ฌ๋ก€์—์„œ๋Š” `Text` ๋Œ€์‹  `str`์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ Python2์™€ Python3 ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- Python3์—๋Š” `unicode`๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์ด๋Ÿฌํ•œ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์ด Python3์™€ Python2์—์„œ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. - ```python - from typing import Text - ... - def py2_compatible(x: Text) -> Text: - ... - def py3_only(x: str) -> str: +- ๋ถ€์ ์ ˆํ•œ ์˜ˆ + +```python +def py2_code(x: str) -> unicode: ... - ``` +``` -- Type์ด byte ๋˜๋Š” Text ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ ์ ˆํ•œ Text Type๊ณผ ํ•จ๊ป˜ `Union`์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +If the type can be either bytes or text, use `Union`, with the appropriate text +type. - ```python - from typing import Text, Union - ... - def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: +```python +from typing import Text, Union +... +def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: ... - def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: +def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: ... - ``` +``` - ํ•จ์ˆ˜์˜ ๋ชจ๋“  string Type์ด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฝ์šฐ(์˜ˆ, ๋ฐ˜ํ™˜ Type์ด ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ธ์ž Type๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ) [AnyStr](#s3.19.10-type-var)๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.5 \353\271\210 \354\244\204.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.5 \353\271\210 \354\244\204.md" index 8a34c90..7097185 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.5 \353\271\210 \354\244\204.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.5 \353\271\210 \354\244\204.md" @@ -6,3 +6,7 @@ - ๊ฐ ๋ฉ”์†Œ๋“œ ์„ ์–ธ ๋˜๋Š” `class` ์ค„๊ณผ ์ “ ๋ฒˆ์งธ ๋ฉ”์†Œ๋“œ ์„ ์–ธ ์‹œ ๊ทธ ์‚ฌ์ด์—๋Š” ํ•œ ๊ฐœ์˜ ๋นˆ ์ค„์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - `def` ์ค„ ์ดํ›„์—๋Š” ๋นˆ ์ค„์ด ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ ์‚ฌ์ด์— ๊ฐœ๋ฐœ์ž์˜ ํŒ๋‹จํ•˜์— ์ ์ ˆํ•˜๊ฒŒ ํ•œ ๊ฐœ์˜ ๋นˆ ์ค„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + +- ๋นˆ ์ค„์„ ์ •์˜์— ๊ณ ์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ๋ฐ ๋ฉ”์„œ๋“œ ์ •์˜ ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๊ด€๋ จ ์ฃผ์„์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- comment๊ฐ€ Docstring์˜ ์ผ๋ถ€๋กœ ๋” ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.8 \354\243\274\354\204\235\352\263\274 docstring.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.8 \354\243\274\354\204\235\352\263\274 docstring.md" index c690825..480875d 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.8 \354\243\274\354\204\235\352\263\274 docstring.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/3.8 \354\243\274\354\204\235\352\263\274 docstring.md" @@ -65,6 +65,10 @@ - ๋ฐ˜ํ™˜๊ฐ’์˜ ์ž๋ฃŒํ˜•๊ณผ ์˜๋ฏธ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜๊ฐ€ None๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์ด ์„น์…˜์€ ํ•„์š”์—†์Šต๋‹ˆ๋‹ค. - ๋˜ํ•œ ๋งŒ์•ฝ docstring์ด Returns ๋‚˜ Yields๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜(e.g. `"""Returns row from Bigtable as a tuple of strings."""`) ์ถฉ๋ถ„ํ•œ ์„ค๋ช…์ด ์ œ๊ณต๋œ๋‹ค๋ฉด ์ƒ๋žต ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Tuple ๋ฐ˜ํ™˜ ๊ฐ’์„ ๊ฐœ๋ณ„ ์ด๋ฆ„์ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ฐ˜ํ™˜ ๊ฐ’์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ž์ฃผ ๋ฌธ์„œํ™”ํ•˜๋Š” 'NumPy style' ([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html))์„ ๋ชจ๋ฐฉํ•˜์ง€ ๋งˆ์„ธ์š”. (Tuple๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.) +- ๋Œ€์‹ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ธฐ์ˆ ํ•˜์„ธ์š”. + - "Returns a tuple (mat_a, mat_b), where mat_a is ..., and ...". +- Docstring์˜ ๋ณด์กฐ ์ด๋ฆ„์€ ํ•จ์ˆ˜ ๋ณธ๋ฌธ์— ์‚ฌ์šฉ๋œ ๋‚ด๋ถ€ ์ด๋ฆ„๊ณผ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. (ํ•ด๋‹น ์ด๋ฆ„์€ API์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.) @@ -78,7 +82,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -88,8 +92,8 @@ table_handle: An open smalltable.Table instance. keys: A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. - require_all_keys: Optional; If require_all_keys is True only - rows with values set for all keys will be returned. + require_all_keys: If True only rows with values set for all keys will be + returned. Returns: A dict mapping keys to the corresponding table row data @@ -115,7 +119,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -128,8 +132,7 @@ A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. require_all_keys: - Optional; If require_all_keys is True only rows with values set - for all keys will be returned. + If True only rows with values set for all keys will be returned. Returns: A dict mapping keys to the corresponding table row data diff --git "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/ReadMe.md" "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/ReadMe.md" index 3e9239f..41c38b2 100644 --- "a/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/ReadMe.md" +++ "b/Google Python Style Guide/3. Python \354\212\244\355\203\200\354\235\274 \352\267\234\354\271\231/ReadMe.md" @@ -232,6 +232,10 @@ - `def` ์ค„ ์ดํ›„์—๋Š” ๋นˆ ์ค„์ด ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ ์‚ฌ์ด์— ๊ฐœ๋ฐœ์ž์˜ ํŒ๋‹จํ•˜์— ์ ์ ˆํ•˜๊ฒŒ ํ•œ ๊ฐœ์˜ ๋นˆ ์ค„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๋นˆ ์ค„์„ ์ •์˜์— ๊ณ ์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ๋ฐ ๋ฉ”์„œ๋“œ ์ •์˜ ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๊ด€๋ จ ์ฃผ์„์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- comment๊ฐ€ Docstring์˜ ์ผ๋ถ€๋กœ ๋” ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. + --- @@ -435,6 +439,10 @@ - ๋ฐ˜ํ™˜๊ฐ’์˜ ์ž๋ฃŒํ˜•๊ณผ ์˜๋ฏธ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜๊ฐ€ None๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์ด ์„น์…˜์€ ํ•„์š”์—†์Šต๋‹ˆ๋‹ค. - ๋˜ํ•œ ๋งŒ์•ฝ docstring์ด Returns ๋‚˜ Yields๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜(e.g. `"""Returns row from Bigtable as a tuple of strings."""`) ์ถฉ๋ถ„ํ•œ ์„ค๋ช…์ด ์ œ๊ณต๋œ๋‹ค๋ฉด ์ƒ๋žต ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Tuple ๋ฐ˜ํ™˜ ๊ฐ’์„ ๊ฐœ๋ณ„ ์ด๋ฆ„์ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ฐ˜ํ™˜ ๊ฐ’์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ž์ฃผ ๋ฌธ์„œํ™”ํ•˜๋Š” 'NumPy style' ([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html))์„ ๋ชจ๋ฐฉํ•˜์ง€ ๋งˆ์„ธ์š”. (Tuple๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.) +- ๋Œ€์‹ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ธฐ์ˆ ํ•˜์„ธ์š”. + - "Returns a tuple (mat_a, mat_b), where mat_a is ..., and ...". +- Docstring์˜ ๋ณด์กฐ ์ด๋ฆ„์€ ํ•จ์ˆ˜ ๋ณธ๋ฌธ์— ์‚ฌ์šฉ๋œ ๋‚ด๋ถ€ ์ด๋ฆ„๊ณผ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. (ํ•ด๋‹น ์ด๋ฆ„์€ API์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.) @@ -448,7 +456,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -458,8 +466,8 @@ table_handle: An open smalltable.Table instance. keys: A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. - require_all_keys: Optional; If require_all_keys is True only - rows with values set for all keys will be returned. + require_all_keys: If True only rows with values set for all keys will be + returned. Returns: A dict mapping keys to the corresponding table row data @@ -485,7 +493,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, - ) -> Mapping[bytes, Tuple[str]]: + ) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -498,8 +506,7 @@ A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. require_all_keys: - Optional; If require_all_keys is True only rows with values set - for all keys will be returned. + If True only rows with values set for all keys will be returned. Returns: A dict mapping keys to the corresponding table row data @@ -645,7 +652,7 @@ if i & (i-1) == 0: # True if i is 0 or a power of 2. ``` - ํ•˜๋‚˜์˜ ํŒŒ์ผ์—๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. `'` ๋˜๋Š” `"` ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ทธ๊ฒƒ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”. -- ๋‹ค๋งŒ `\\` ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. `gpylint` ๊ฐ€ ์ด๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. +- ๋‹ค๋งŒ backslash-escape ๋”ฐ์Œํ‘œ ๋ฌธ์ž ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ™์€ ํŒŒ์ผ์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. - ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ @@ -970,15 +977,19 @@ if i & (i-1) == 0: # True if i is 0 or a power of 2. --- + ### 3.15 ์ ‘๊ทผ ์ œ์–ด -- ๋งŒ์•ฝ ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ ๋ฌด์‹œํ•  ๊ฒฝ์šฐ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ถ”๊ฐ€์ ์ธ ๋น„์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜ ๋Œ€์‹ ์— public ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด `property`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ๋ฒ•์„ ์ผ๊ด€์ ์œผ๋กœ ์œ ์ง€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - -- ๋ฐ˜๋ฉด์— ์ ‘๊ทผ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋น„์šฉ์ด ํฐ ๊ฒฝ์šฐ, `get_foo()` ์™€ `set_foo()`์™€ ๊ฐ™์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ([๋„ค์ด๋ฐ](#s3.16-naming) ๊ฐ€์ด๋“œ ๋ผ์ธ์„ ์ฐธ๊ณ ํ•˜๋ผ)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋งŒ์•ฝ ์ „์— ํ–ˆ๋˜ ํ–‰๋™์ด property๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์„ ํ—ˆ๋ฝํ–ˆ๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์ ‘๊ทผ์ œ์–ด์ž ํ•จ์ˆ˜๋ฅผ property์™€ ๋ฌถ์ง€๋งˆ์„ธ์š”. -- ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ์—ฌ์ „ํžˆ ๋ณ€์ˆ˜์— ์˜ค๋ž˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋ ค ์‹œ๋„ํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ๋ˆˆ์— ๋ณด์ด๊ฒŒ ๋ถ€์ˆ˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- Getter/Setter ํ•จ์ˆ˜(์ ‘๊ทผ์ž ๋ฐ ๋ณ€๊ฒฝ์ž๋ผ๊ณ ๋„ํ•จ)๋Š” ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์˜๋ฏธ ์žˆ๋Š” ์—ญํ• ์ด๋‚˜ ๋™์ž‘์„ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํŠนํžˆ ํ˜„์žฌ ๋˜๋Š” ํ•ฉ๋ฆฌ์ ์ธ ๋ฏธ๋ž˜์— ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ๋น„์šฉ์ด ์ƒ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ ์Œ์˜ getter/setter๊ฐ€ ๋‹จ์ˆœํžˆ ๋‚ด๋ถ€ ์†์„ฑ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฒฝ์šฐ ๋‚ด๋ถ€ ์†์„ฑ์€ ๋Œ€์‹  ๊ณต๊ฐœ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์— ๋น„ํ•ด ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ผ๋ถ€ ์ƒํƒœ๊ฐ€ ๋ฌดํšจํ™”๋˜๊ฑฐ๋‚˜ ๋‹ค์‹œ ์ž‘์„ฑ๋จ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋Š” setter ํ•จ์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•จ์ˆ˜ ํ˜ธ์ถœ์€ ์ž ์žฌ์ ์œผ๋กœ ์‚ฌ์†Œํ•˜์ง€ ์•Š์€ ์ž‘์—…์ด ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์Œ์„ ์•”์‹œํ•ฉ๋‹ˆ๋‹ค. +- ๋˜ํ•œ ๊ฐ„๋‹จํ•œ ๋…ผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๋” ์ด์ƒ getter/setter๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋„๋ก ๋ฆฌํŒฉํ† ๋งํ•  ๋•Œ [properties](#properties) ์˜ต์…˜์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- Getter/Setter๋Š” `get_foo()`, `set_foo()`์™€ ๊ฐ™์€ [Naming](#s3.16-naming) ์ง€์นจ์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด์ „ ๋™์ž‘์—์„œ property์„ ํ†ตํ•œ ์—‘์„ธ์Šค๊ฐ€ ํ—ˆ์šฉ๋œ ๊ฒฝ์šฐ ์ƒˆ getter/setterํ•จ์ˆ˜๋ฅผ property์— ๋ฐ”์ธ๋”ฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์—ฌ์ „ํžˆ ์ด์ „ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋ˆˆ์— ๋„๊ฒŒ ์ค‘๋‹จ๋˜์–ด ๋ณต์žก์„ฑ์˜ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. --- @@ -1240,13 +1251,15 @@ if __name__ == '__main__': #### 3.19.3 ์ „๋ฐฉ์„ ์–ธ -- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ์•„์ง ์ •์˜๋˜์ง€ ์•Š์€ ๋™์ผํ•œ ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ, ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด์— ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Š” ์•„๋ž˜์— ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) `from __future__ import annotations`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๊ฐ„๋‹จํ•œ ๊ฒฝ์šฐ์—๋Š” ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํด๋ž˜์Šค ์ด๋ฆ„์— ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ```python +from __future__ import annotations + class MyClass: - def __init__(self, - stack: List["MyClass"]) -> None: + def __init__(self, stack: Sequence[MyClass]) -> None: ``` @@ -1398,54 +1411,41 @@ c = (1, "2", 3.5) # type: Tuple[int, str, float] - String ์ฃผ์„์— ๋Œ€ํ•œ ์ ์ ˆํ•œ Type์€ ์ฝ”๋“œ์˜ ์šฉ๋„์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. -- Python 3 ํ˜ธํ™˜ ์ฝ”๋“œ์ผ ๊ฒฝ์šฐ `str`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - `Text`๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. - - ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - -- Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - - ๋“œ๋ฏ„ ๊ฒฝ์šฐ์— `str`์€ ์ ์ ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - - ์ผ๋ฐ˜์ ์œผ๋กœ ๋‘ Python ๋ฒ„์ „ ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•์Šต๋‹ˆ๋‹ค. Python 3์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” `unicode`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. - -- ์ด๋Ÿฐ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์€ Python ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. - -- ๋ถ€์ ์ ˆํ•œ ์˜ˆ +- `str`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜์ง€๋งŒ `Text`๋„ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +- ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ(Python 2์—์„œ๋Š” `str` ๋˜๋Š” `unicode`, Python3์—์„œ๋Š” `str`)๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Python 2 ํ˜ธํ™˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ `Text`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - ```python - def py2_code(x: str) -> unicode: +```python +def deals_with_text_data_in_py3(x: str) -> str: ... - ``` - -- ์ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด `bytes`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. - - ```python - def deals_with_binary_data(x: bytes) -> bytes: +def deals_with_binary_data(x: bytes) -> bytes: ... - ``` +def py2_compatible_text_data_processor(x: Text) -> Text: + ... +``` -- Python 2์—์„œ Text ๋ฐ์ดํ„ฐ(`str`, `unicode`๋Š” python 2, `str`์€ python 3)๋Š” `Text`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. -- Python 3์—์„œ๋งŒ Text ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ `str`๋ฅผ ์„ ํƒํ•˜์„ธ์š”. +- ์ผ๋ถ€ ํ”ํ•˜์ง€ ์•Š์€ Python2 ํ˜ธํ™˜์„ฑ ์‚ฌ๋ก€์—์„œ๋Š” `Text` ๋Œ€์‹  `str`์ด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ Python2์™€ Python3 ๊ฐ„์— ๋ฐ˜ํ™˜ ์œ ํ˜•์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜ธํ™˜์„ฑ์„ ๋•๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- Python3์—๋Š” `unicode`๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์ด๋Ÿฌํ•œ ๋ถˆ์ผ์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ์ด์œ ๋Š” `str`์ด Python3์™€ Python2์—์„œ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. - ```python - from typing import Text - ... - def py2_compatible(x: Text) -> Text: - ... - def py3_only(x: str) -> str: +- ๋ถ€์ ์ ˆํ•œ ์˜ˆ + +```python +def py2_code(x: str) -> unicode: ... - ``` +``` -- Type์ด byte ๋˜๋Š” Text ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ ์ ˆํ•œ Text Type๊ณผ ํ•จ๊ป˜ `Union`์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +If the type can be either bytes or text, use `Union`, with the appropriate text +type. - ```python - from typing import Text, Union - ... - def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: +```python +from typing import Text, Union +... +def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: ... - def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: +def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: ... - ``` +``` - ํ•จ์ˆ˜์˜ ๋ชจ๋“  string Type์ด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฝ์šฐ(์˜ˆ, ๋ฐ˜ํ™˜ Type์ด ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ธ์ž Type๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ) [AnyStr](#s3.19.10-type-var)๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. diff --git a/Original.md b/Original.md index 67d38b2..09df969 100644 --- a/Original.md +++ b/Original.md @@ -11,155 +11,77 @@ See README.md for details.
Table of Contents -- [Google Python Style Guide](#google-python-style-guide) - - [1 Background](#1-background) - - [2 Python Language Rules](#2-python-language-rules) - - [2.1 Lint](#21-lint) - - [2.1.1 Definition](#211-definition) - - [2.1.2 Pros](#212-pros) - - [2.1.3 Cons](#213-cons) - - [2.1.4 Decision](#214-decision) - - [2.2 Imports](#22-imports) - - [2.2.1 Definition](#221-definition) - - [2.2.2 Pros](#222-pros) - - [2.2.3 Cons](#223-cons) - - [2.2.4 Decision](#224-decision) - - [2.3 Packages](#23-packages) - - [2.3.1 Pros](#231-pros) - - [2.3.2 Cons](#232-cons) - - [2.3.3 Decision](#233-decision) - - [2.4 Exceptions](#24-exceptions) - - [2.4.1 Definition](#241-definition) - - [2.4.2 Pros](#242-pros) - - [2.4.3 Cons](#243-cons) - - [2.4.4 Decision](#244-decision) - - [2.5 Global variables](#25-global-variables) - - [2.5.1 Definition](#251-definition) - - [2.5.2 Pros](#252-pros) - - [2.5.3 Cons](#253-cons) - - [2.5.4 Decision](#254-decision) - - [2.6 Nested/Local/Inner Classes and Functions](#26-nestedlocalinner-classes-and-functions) - - [2.6.1 Definition](#261-definition) - - [2.6.2 Pros](#262-pros) - - [2.6.3 Cons](#263-cons) - - [2.6.4 Decision](#264-decision) - - [2.7 Comprehensions & Generator Expressions](#27-comprehensions--generator-expressions) - - [2.7.1 Definition](#271-definition) - - [2.7.2 Pros](#272-pros) - - [2.7.3 Cons](#273-cons) - - [2.7.4 Decision](#274-decision) - - [2.8 Default Iterators and Operators](#28-default-iterators-and-operators) - - [2.8.1 Definition](#281-definition) - - [2.8.2 Pros](#282-pros) - - [2.8.3 Cons](#283-cons) - - [2.8.4 Decision](#284-decision) - - [2.9 Generators](#29-generators) - - [2.9 Definition](#29-definition) - - [2.9.2 Pros](#292-pros) - - [2.9.3 Cons](#293-cons) - - [2.9.4 Decision](#294-decision) - - [2.10 Lambda Functions](#210-lambda-functions) - - [2.10.1 Definition](#2101-definition) - - [2.10.2 Pros](#2102-pros) - - [2.10.3 Cons](#2103-cons) - - [2.10.4 Decision](#2104-decision) - - [2.11 Conditional Expressions](#211-conditional-expressions) - - [2.11.1 Definition](#2111-definition) - - [2.11.2 Pros](#2112-pros) - - [2.11.3 Cons](#2113-cons) - - [2.11.4 Decision](#2114-decision) - - [2.12 Default Argument Values](#212-default-argument-values) - - [2.12.1 Definition](#2121-definition) - - [2.12.2 Pros](#2122-pros) - - [2.12.3 Cons](#2123-cons) - - [2.12.4 Decision](#2124-decision) - - [2.13 Properties](#213-properties) - - [2.13.1 Definition](#2131-definition) - - [2.13.2 Pros](#2132-pros) - - [2.13.3 Cons](#2133-cons) - - [2.13.4 Decision](#2134-decision) - - [2.14 True/False Evaluations](#214-truefalse-evaluations) - - [2.14.1 Definition](#2141-definition) - - [2.14.2 Pros](#2142-pros) - - [2.14.3 Cons](#2143-cons) - - [2.14.4 Decision](#2144-decision) - - [2.16 Lexical Scoping](#216-lexical-scoping) - - [2.16.1 Definition](#2161-definition) - - [2.16.2 Pros](#2162-pros) - - [2.16.3 Cons](#2163-cons) - - [2.16.4 Decision](#2164-decision) - - [2.17 Function and Method Decorators](#217-function-and-method-decorators) - - [2.17.1 Definition](#2171-definition) - - [2.17.2 Pros](#2172-pros) - - [2.17.3 Cons](#2173-cons) - - [2.17.4 Decision](#2174-decision) - - [2.18 Threading](#218-threading) - - [2.19 Power Features](#219-power-features) - - [2.19.1 Definition](#2191-definition) - - [2.19.2 Pros](#2192-pros) - - [2.19.3 Cons](#2193-cons) - - [2.19.4 Decision](#2194-decision) - - [2.20 Modern Python: Python 3 and from \_\_future\_\_ imports](#220-modern-python-python-3-and-from-__future__-imports) - - [2.20.1 Definition](#2201-definition) - - [2.20.2 Pros](#2202-pros) - - [2.20.3 Cons](#2203-cons) - - [2.20.4 Decision](#2204-decision) - - [from \_\_future\_\_ imports](#from-__future__-imports) - - [The six, future, and past libraries](#the-six-future-and-past-libraries) - - [2.21 Type Annotated Code](#221-type-annotated-code) - - [2.21.1 Definition](#2211-definition) - - [2.21.2 Pros](#2212-pros) - - [2.21.3 Cons](#2213-cons) - - [2.21.4 Decision](#2214-decision) - - [3 Python Style Rules](#3-python-style-rules) - - [3.1 Semicolons](#31-semicolons) - - [3.2 Line length](#32-line-length) - - [3.3 Parentheses](#33-parentheses) - - [3.4 Indentation](#34-indentation) - - [3.4.1 Trailing commas in sequences of items?](#341-trailing-commas-in-sequences-of-items) - - [3.5 Blank Lines](#35-blank-lines) - - [3.6 Whitespace](#36-whitespace) - - [3.7 Shebang Line](#37-shebang-line) - - [3.8 Comments and Docstrings](#38-comments-and-docstrings) - - [3.8.1 Docstrings](#381-docstrings) - - [3.8.2 Modules](#382-modules) - - [3.8.3 Functions and Methods](#383-functions-and-methods) - - [3.8.4 Classes](#384-classes) - - [3.8.5 Block and Inline Comments](#385-block-and-inline-comments) - - [3.8.6 Punctuation, Spelling, and Grammar](#386-punctuation-spelling-and-grammar) - - [3.10 Strings](#310-strings) - - [3.10.1 Logging](#3101-logging) - - [3.10.2 Error Messages](#3102-error-messages) - - [3.11 Files, Sockets, and similar Stateful Resources](#311-files-sockets-and-similar-stateful-resources) - - [3.12 TODO Comments](#312-todo-comments) - - [3.13 Imports formatting](#313-imports-formatting) - - [3.14 Statements](#314-statements) - - [3.15 Accessors](#315-accessors) - - [3.16 Naming](#316-naming) - - [3.16.1 Names to Avoid](#3161-names-to-avoid) - - [3.16.2 Naming Conventions](#3162-naming-conventions) - - [3.16.3 File Naming](#3163-file-naming) - - [3.16.4 Guidelines derived from Guido's Recommendations](#3164-guidelines-derived-from-guidos-recommendations) - - [3.16.5 Mathematical Notation](#3165-mathematical-notation) - - [3.17 Main](#317-main) - - [3.18 Function length](#318-function-length) - - [3.19 Type Annotations](#319-type-annotations) - - [3.19.1 General Rules](#3191-general-rules) - - [3.19.2 Line Breaking](#3192-line-breaking) - - [3.19.3 Forward Declarations](#3193-forward-declarations) - - [3.19.4 Default Values](#3194-default-values) - - [3.19.5 NoneType](#3195-nonetype) - - [3.19.6 Type Aliases](#3196-type-aliases) - - [3.19.7 Ignoring Types](#3197-ignoring-types) - - [3.19.8 Typing Variables](#3198-typing-variables) - - [3.19.9 Tuples vs Lists](#3199-tuples-vs-lists) - - [3.19.10 TypeVars](#31910-typevars) - - [3.19.11 String types](#31911-string-types) - - [3.19.12 Imports For Typing](#31912-imports-for-typing) - - [3.19.13 Conditional Imports](#31913-conditional-imports) - - [3.19.14 Circular Dependencies](#31914-circular-dependencies) - - [3.19.15 Generics](#31915-generics) - - [4 Parting Words](#4-parting-words) +- [1 Background](#s1-background) +- [2 Python Language Rules](#s2-python-language-rules) + * [2.1 Lint](#s2.1-lint) + * [2.2 Imports](#s2.2-imports) + * [2.3 Packages](#s2.3-packages) + * [2.4 Exceptions](#s2.4-exceptions) + * [2.5 Global variables](#s2.5-global-variables) + * [2.6 Nested/Local/Inner Classes and Functions](#s2.6-nested) + * [2.7 Comprehensions & Generator Expressions](#s2.7-comprehensions) + * [2.8 Default Iterators and Operators](#s2.8-default-iterators-and-operators) + * [2.9 Generators](#s2.9-generators) + * [2.10 Lambda Functions](#s2.10-lambda-functions) + * [2.11 Conditional Expressions](#s2.11-conditional-expressions) + * [2.12 Default Argument Values](#s2.12-default-argument-values) + * [2.13 Properties](#s2.13-properties) + * [2.14 True/False Evaluations](#s2.14-truefalse-evaluations) + * [2.16 Lexical Scoping](#s2.16-lexical-scoping) + * [2.17 Function and Method Decorators](#s2.17-function-and-method-decorators) + * [2.18 Threading](#s2.18-threading) + * [2.19 Power Features](#s2.19-power-features) + * [2.20 Modern Python: from \_\_future\_\_ imports](#s2.20-modern-python) + * [2.21 Type Annotated Code](#s2.21-type-annotated-code) +- [3 Python Style Rules](#s3-python-style-rules) + * [3.1 Semicolons](#s3.1-semicolons) + * [3.2 Line length](#s3.2-line-length) + * [3.3 Parentheses](#s3.3-parentheses) + * [3.4 Indentation](#s3.4-indentation) + + [3.4.1 Trailing commas in sequences of items?](#s3.4.1-trailing-commas) + * [3.5 Blank Lines](#s3.5-blank-lines) + * [3.6 Whitespace](#s3.6-whitespace) + * [3.7 Shebang Line](#s3.7-shebang-line) + * [3.8 Comments and Docstrings](#s3.8-comments-and-docstrings) + + [3.8.1 Docstrings](#s3.8.1-comments-in-doc-strings) + + [3.8.2 Modules](#s3.8.2-comments-in-modules) + + [3.8.3 Functions and Methods](#s3.8.3-functions-and-methods) + + [3.8.4 Classes](#s3.8.4-comments-in-classes) + + [3.8.5 Block and Inline Comments](#s3.8.5-block-and-inline-comments) + + [3.8.6 Punctuation, Spelling, and Grammar](#s3.8.6-punctuation-spelling-and-grammar) + * [3.10 Strings](#s3.10-strings) + + [3.10.1 Logging](#s3.10.1-logging) + + [3.10.2 Error Messages](#s3.10.2-error-messages) + * [3.11 Files, Sockets, and similar Stateful Resources](#s3.11-files-sockets-closeables) + * [3.12 TODO Comments](#s3.12-todo-comments) + * [3.13 Imports formatting](#s3.13-imports-formatting) + * [3.14 Statements](#s3.14-statements) + * [3.15 Accessors](#s3.15-accessors) + * [3.16 Naming](#s3.16-naming) + + [3.16.1 Names to Avoid](#s3.16.1-names-to-avoid) + + [3.16.2 Naming Conventions](#s3.16.2-naming-conventions) + + [3.16.3 File Naming](#s3.16.3-file-naming) + + [3.16.4 Guidelines derived from Guido's Recommendations](#s3.16.4-guidelines-derived-from-guidos-recommendations) + * [3.17 Main](#s3.17-main) + * [3.18 Function length](#s3.18-function-length) + * [3.19 Type Annotations](#s3.19-type-annotations) + + [3.19.1 General Rules](#s3.19.1-general-rules) + + [3.19.2 Line Breaking](#s3.19.2-line-breaking) + + [3.19.3 Forward Declarations](#s3.19.3-forward-declarations) + + [3.19.4 Default Values](#s3.19.4-default-values) + + [3.19.5 NoneType](#s3.19.5-nonetype) + + [3.19.6 Type Aliases](#s3.19.6-type-aliases) + + [3.19.7 Ignoring Types](#s3.19.7-ignoring-types) + + [3.19.8 Typing Variables](#s3.19.8-typing-variables) + + [3.19.9 Tuples vs Lists](#s3.19.9-tuples-vs-lists) + + [3.19.10 TypeVars](#s3.19.10-typevars) + + [3.19.11 String types](#s3.19.11-string-types) + + [3.19.12 Imports For Typing](#s3.19.12-imports-for-typing) + + [3.19.13 Conditional Imports](#s3.19.13-conditional-imports) + + [3.19.14 Circular Dependencies](#s3.19.14-circular-dependencies) + + [3.19.15 Generics](#s3.19.15-generics) + + [3.19.16 Build Dependencies](#s3.19.16-build-dependencies) +- [4 Parting Words](#4-parting-words)
@@ -290,9 +212,10 @@ that the arguments are actually unused. ### 2.2 Imports Use `import` statements for packages and modules only, not for individual -classes or functions. Imports from the [typing module](#typing-imports), +classes or functions. Classes imported from the +[typing module](#typing-imports), [typing_extensions module](https://github.com/python/typing/tree/master/typing_extensions), -and the +and redirects from the [six.moves module](https://six.readthedocs.io/#module-six.moves) are exempt from this rule. @@ -384,31 +307,32 @@ All new code should import each module by its full package name. Imports should be as follows: -Yes: - ```python -# Reference absl.flags in code with the complete name (verbose). -import absl.flags -from doctor.who import jodie +Yes: + # Reference absl.flags in code with the complete name (verbose). + import absl.flags + from doctor.who import jodie -FLAGS = absl.flags.FLAGS + FLAGS = absl.flags.FLAGS ``` ```python -# Reference flags in code with just the module name (common). -from absl import flags -from doctor.who import jodie +Yes: + # Reference flags in code with just the module name (common). + from absl import flags + from doctor.who import jodie -FLAGS = flags.FLAGS + FLAGS = flags.FLAGS ``` -No: _(assume this file lives in `doctor/who/` where `jodie.py` also exists)_ +*(assume this file lives in `doctor/who/` where `jodie.py` also exists)* ```python -# Unclear what module the author wanted and what will be imported. The actual -# import behavior depends on external factors controlling sys.path. -# Which possible jodie module did the author intend to import? -import jodie +No: + # Unclear what module the author wanted and what will be imported. The actual + # import behavior depends on external factors controlling sys.path. + # Which possible jodie module did the author intend to import? + import jodie ``` The directory the main binary is located in should not be assumed to be in @@ -517,9 +441,10 @@ Exceptions must follow certain conditions: return port ``` + - Libraries or packages may define their own exceptions. When doing so they must inherit from an existing exception class. Exception names should end in - `Error` and should not introduce stutter (`foo.FooError`). + `Error` and should not introduce repetition (`foo.FooError`). - Never use catch-all `except:` statements, or catch `Exception` or `StandardError`, unless you are @@ -1046,8 +971,10 @@ No: def foo(a, b: Mapping = {}): # Could still get passed to unchecked code ### 2.13 Properties -Use properties for accessing or setting data where you would normally have used -simple, lightweight accessor or setter methods. +Properties may be used to control getting or setting attributes that require +trivial computations or logic. Property implementations must match the general +expectations of regular attribute access: that they are cheap, straightforward, +and unsurprising. @@ -1056,7 +983,7 @@ simple, lightweight accessor or setter methods. #### 2.13.1 Definition A way to wrap method calls for getting and setting an attribute as a standard -attribute access when the computation is lightweight. +attribute access. @@ -1064,12 +991,12 @@ attribute access when the computation is lightweight. #### 2.13.2 Pros -Readability is increased by eliminating explicit get and set method calls for -simple attribute access. Allows calculations to be lazy. Considered the Pythonic -way to maintain the interface of a class. In terms of performance, allowing -properties bypasses needing trivial accessor methods when a direct variable -access is reasonable. This also allows accessor methods to be added in the -future without breaking the interface. +* Allows for an attribute access and assignment API rather than + [getter and setter](#getters-and-setters) method calls. +* Can be used to make an attribute read-only. +* Allows calculations to be lazy. +* Provides a way to maintain the public interface of a class when the + internals evolve independently of class users. @@ -1077,8 +1004,8 @@ future without breaking the interface. #### 2.13.3 Cons -Can hide side-effects much like operator overloading. Can be confusing for -subclasses. +* Can hide side-effects much like operator overloading. +* Can be confusing for subclasses. @@ -1086,58 +1013,22 @@ subclasses. #### 2.13.4 Decision -Use properties in new code to access or set data where you would normally have -used lightweight accessor or setter methods. Properties should be created with -the `@property` [decorator](#s2.17-function-and-method-decorators). - -Inheritance with properties can be non-obvious if the property itself is not -overridden. Thus one must make sure that accessor methods are called indirectly -to ensure methods overridden in subclasses are called by the property (using the -[template method design pattern](https://en.wikipedia.org/wiki/Template_method_pattern)). - -```python -Yes: import math - - class Square: - """A square with two properties: a writable area and a read-only perimeter. - - To use: - >>> sq = Square(3) - >>> sq.area - 9 - >>> sq.perimeter - 12 - >>> sq.area = 16 - >>> sq.side - 4 - >>> sq.perimeter - 16 - """ - - def __init__(self, side: float): - self.side = side +Properties are allowed, but, like operator overloading, should only be used when +necessary and match the expectations of typical attribute access; follow the +[getters and setters](#getters-and-setters) rules otherwise. - @property - def area(self) -> float: - """Area of the square.""" - return self._get_area() +For example, using a property to simply both get and set an internal attribute +isn't allowed: there is no computation occurring, so the property is unnecessary +([make the attribute public instead](#getters-and-setters)). In comparison, +using a property to control attribute access or to calculate a *trivially* +derived value is allowed: the logic is simple and unsurprising. - @area.setter - def area(self, area: float): - self._set_area(area) +Properties should be created with the `@property` +[decorator](#s2.17-function-and-method-decorators). Manually implementing a +property descriptor is considered a [power feature](#power-features). - def _get_area(self) -> float: - """Indirect accessor to calculate the 'area' property.""" - return self.side ** 2 - - def _set_area(self, area: float): - """Indirect setter to set the 'area' property.""" - self.side = math.sqrt(area) - - @property - def perimeter(self) -> float: - return self.side * 4 -``` +Inheritance with properties can be non-obvious. Do not use properties to +implement computations a subclass may ever want to override and extend. @@ -1205,9 +1096,6 @@ Use the "implicit" false if possible, e.g., `if foo:` rather than `if foo != Yes: if not users: print('no users') - if foo == 0: - self.handle_zero() - if i % 10 == 0: self.handle_multiple_of_ten() @@ -1220,9 +1108,6 @@ Use the "implicit" false if possible, e.g., `if foo:` rather than `if foo != No: if len(users) == 0: print('no users') - if foo is not None and not foo: - self.handle_zero() - if not i % 10: self.handle_multiple_of_ten() @@ -1232,6 +1117,10 @@ Use the "implicit" false if possible, e.g., `if foo:` rather than `if foo != - Note that `'0'` (i.e., `0` as string) evaluates to true. +- Note that Numpy arrays may raise an exception in an implicit boolean + context. Prefer the `.size` attribute when testing emptiness of a `np.array` + (e.g. `if not users.size`). + @@ -1361,8 +1250,9 @@ eliminate some repetitive code, enforce invariants, etc. Decorators can perform arbitrary operations on a function's arguments or return values, resulting in surprising implicit behavior. Additionally, decorators -execute at import time. Failures in decorator code are pretty much impossible to -recover from. +execute at object definition time. For module-level objects (classes, module +functions, ...) this happens at import time. Failures in decorator code are +pretty much impossible to recover from. @@ -1464,11 +1354,10 @@ to use (for example, `abc.ABCMeta`, `dataclasses`, and `enum`). -### 2.20 Modern Python: Python 3 and from \_\_future\_\_ imports +### 2.20 Modern Python: from \_\_future\_\_ imports -Python 3 is here! While not every project is ready to use it yet, -all code should be written to be 3 compatible (and tested under 3 when -possible). +New language version semantic changes may be gated behind a special future +import to enable them on a per-file basis within earlier runtimes. @@ -1476,10 +1365,9 @@ possible). #### 2.20.1 Definition -Python 3 is a significant change in the Python language. While existing code is -often written with 2.7 in mind, there are some simple things to do to make code -more explicit about its intentions and thus better prepared for use under Python -3 without modification. +Being able to turn on some of the more modern features via `from __future__ +import` statements allows early use of features from expected future Python +versions. @@ -1487,8 +1375,11 @@ more explicit about its intentions and thus better prepared for use under Python #### 2.20.2 Pros -Code written with Python 3 in mind is more explicit and easier to get running -under Python 3 once all of the dependencies of your project are ready. +This has proven to make runtime version upgrades smoother as changes can be made +on a per-file basis while declaring compatibility and preventing regressions +within those files. Modern code is more maintainable as it is less likely to +accumulate technical debt that will be problematic during future runtime +upgrades. @@ -1496,9 +1387,9 @@ under Python 3 once all of the dependencies of your project are ready. #### 2.20.3 Cons -Some people find the additional boilerplate to be ugly. It's unusual to add -imports to a module that doesn't actually require the features added by the -import. +Such code may not work on very old interpreter versions prior to the +introduction of the needed future statement. The need for this is more common in +projects supporting an extremely wide variety of environments. @@ -1508,9 +1399,18 @@ import. ##### from \_\_future\_\_ imports -Use of `from __future__ import` statements is encouraged. All new code should -contain the following and existing code should be updated to be compatible when -possible: +Use of `from __future__ import` statements is encouraged. It allows a given +source file to start using more modern Python syntax features today. Once you no +longer need to run on a version where the features are hidden behind a +`__future__` import, feel free to remove those lines. + +In code that may execute on versions as old as 3.5 rather than >= 3.7, import: + +```python +from __future__ import generator_stop +``` + +For legacy code with the burden of continuing to support 2.7, import: ```python from __future__ import absolute_import @@ -1518,27 +1418,26 @@ from __future__ import division from __future__ import print_function ``` -For more information on these imports, see -[absolute imports](https://www.python.org/dev/peps/pep-0328/), -[`/` division behavior](https://www.python.org/dev/peps/pep-0238/), and -[the `print` function](https://www.python.org/dev/peps/pep-3105/). - +For more information read the +[Python future statement definitions](https://docs.python.org/3/library/__future__.html) +documentation. -Please don't omit or remove these imports, even if they're not currently used in -the module, unless the code is Python 3 only. It is better to always have the -future imports in all files so that they are not forgotten during later edits -when someone starts using such a feature. +Please don't remove these imports until you are confident the code is only ever +used in a sufficiently modern environment. Even if you do not currently use the +feature a specific future import enables in your code today, keeping it in place +in the file prevents later modifications of the code from inadvertently +depending on the older behavior. -There are other `from __future__` import statements. Use them as you see fit. We -do not include `unicode_literals` in our recommendations as it is not a clear -win due to implicit default codec conversion consequences it introduces in many -places within Python 2.7. Most code is better off with explicit use of `b''` and -`u''` bytes and unicode string literals as necessary. +Use other `from __future__` import statements as you see fit. We did not include +`unicode_literals` in our recommendations for 2.7 as it was not a clear win due +to implicit default codec conversion consequences it introduced in many places +within 2.7. Most dual-version 2-and-3 code was better off with explicit use of +`b''` and `u''` bytes and unicode string literals where necessary. ##### The six, future, and past libraries -When your project needs to actively support use under both Python 2 and 3, use -the [six](https://pypi.org/project/six/), +When your project still needs to support use under both Python 2 and 3, use the +[six](https://pypi.org/project/six/), [future](https://pypi.org/project/future/), and [past](https://pypi.org/project/past/) libraries as you see fit. They exist to make your code cleaner and life easier. @@ -1874,6 +1773,10 @@ definitions. One blank line between method definitions and between the `class` line and the first method. No blank line following a `def` line. Use single blank lines as you judge appropriate within functions or methods. +Blank lines need not be anchored to the definition. For example, related +comments immediately preceding function, class, and method definitions can make +sense. Consider if your comment might be more useful as part of the docstring. + @@ -1943,7 +1846,7 @@ No: x<1 Never use spaces around `=` when passing keyword arguments or defining a default parameter value, with one exception: -[when a type annotation is present](#typing-default-values), _do_ use spaces +[when a type annotation is present](#typing-default-values), *do* use spaces around the `=` for the default parameter value. ```python @@ -2013,7 +1916,7 @@ inline comments. #### 3.8.1 Docstrings -Python uses _docstrings_ to document code. A docstring is a string that is the +Python uses *docstrings* to document code. A docstring is a string that is the first statement in a package, module, class or function. These strings can be extracted automatically through the `__doc__` member of the object and are used by `pydoc`. @@ -2119,7 +2022,14 @@ aptly described using a one-line docstring. returns None, this section is not required. It may also be omitted if the docstring starts with Returns or Yields (e.g. `"""Returns row from Bigtable as a tuple of strings."""`) and the opening sentence is sufficient to - describe return value. + describe the return value. Do not imitate 'NumPy style' + ([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html)), + which frequently documents a tuple return value as if it were multiple + return values with individual names (never mentioning the tuple). Instead, + describe such a return value as: "Returns a tuple (mat_a, mat_b), where + mat_a is ..., and ...". The auxiliary names in the docstring need not + necessarily correspond to any internal names used in the function body (as + those are not part of the API). [*Raises:*](#doc-function-raises) @@ -2134,7 +2044,7 @@ aptly described using a one-line docstring. def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, -) -> Mapping[bytes, Tuple[str]]: +) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -2144,8 +2054,8 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, table_handle: An open smalltable.Table instance. keys: A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. - require_all_keys: Optional; If require_all_keys is True only - rows with values set for all keys will be returned. + require_all_keys: If True only rows with values set for all keys will be + returned. Returns: A dict mapping keys to the corresponding table row data @@ -2171,7 +2081,7 @@ Similarly, this variation on `Args:` with a line break is also allowed: def fetch_smalltable_rows(table_handle: smalltable.Table, keys: Sequence[Union[bytes, str]], require_all_keys: bool = False, -) -> Mapping[bytes, Tuple[str]]: +) -> Mapping[bytes, Tuple[str, ...]]: """Fetches rows from a Smalltable. Retrieves rows pertaining to the given keys from the Table instance @@ -2184,8 +2094,7 @@ def fetch_smalltable_rows(table_handle: smalltable.Table, A sequence of strings representing the key of each table row to fetch. String keys will be UTF-8 encoded. require_all_keys: - Optional; If require_all_keys is True only rows with values set - for all keys will be returned. + If True only rows with values set for all keys will be returned. Returns: A dict mapping keys to the corresponding table row data @@ -2352,7 +2261,7 @@ No: employee_table = '' Be consistent with your choice of string quote character within a file. Pick `'` or `"` and stick with it. It is okay to use the other quote character on a -string to avoid the need to `\\ ` escape within the string. +string to avoid the need to backslash-escape quote characters within the string. ```python Yes: @@ -2747,22 +2656,33 @@ No: - -### 3.15 Accessors -If an accessor function would be trivial, you should use public variables -instead of accessor functions to avoid the extra cost of function calls in -Python. When more functionality is added you can use `property` to keep the -syntax consistent. + +### 3.15 Getters and Setters + +Getter and setter functions (also called accessors and mutators) should be used +when they provide a meaningful role or behavior for getting or setting a +variable's value. + +In particular, they should be used when getting or setting the variable is +complex or the cost is significant, either currently or in a reasonable future. + +If, for example, a pair of getters/setters simply read and write an internal +attribute, the internal attribute should be made public instead. By comparison, +if setting a variable means some state is invalidated or rebuilt, it should be a +setter function. The function invocation hints that a potentially non-trivial +operation is occurring. Alternatively, [properties](#properties) may be an +option when simple logic is needed, or refactoring to no longer need getters and +setters. -On the other hand, if access is more complex, or the cost of accessing the -variable is significant, you should use function calls (following the -[Naming](#s3.16-naming) guidelines) such as `get_foo()` and `set_foo()`. If the -past behavior allowed access through a property, do not bind the new accessor -functions to the property. Any code still attempting to access the variable by -the old method should break visibly so they are made aware of the change in -complexity. +Getters and setters should follow the [Naming](#s3.16-naming) guidelines, such +as `get_foo()` and `set_foo()`. + +If the past behavior allowed access through a property, do not bind the new +getter/setter functions to the property. Any code still attempting to access the +variable by the old method should break visibly so they are made aware of the +change in complexity. @@ -3144,13 +3064,15 @@ def my_function( If you need to use a class name from the same module that is not yet defined -- for example, if you need the class inside the class declaration, or if you use a -class that is defined below -- use a string for the class name. +class that is defined below -- either use `from __future__ import annotations` +for simple cases or use a string for the class name. ```python +from __future__ import annotations + class MyClass: - def __init__(self, - stack: List["MyClass"]) -> None: + def __init__(self, stack: Sequence[MyClass]) -> None: ``` @@ -3161,7 +3083,7 @@ class MyClass: As per [PEP-008](https://www.python.org/dev/peps/pep-0008/#other-recommendations), use -spaces around the `=` _only_ for arguments that have both a type annotation and +spaces around the `=` *only* for arguments that have both a type annotation and a default value. ```python @@ -3342,40 +3264,30 @@ def check_length(x: AnyStr) -> AnyStr: The proper type for annotating strings depends on what versions of Python the code is intended for. -For Python 3 only code, prefer to use `str`. `Text` is also acceptable. Be -consistent in using one or the other. - -For Python 2 compatible code, use `Text`. In some rare cases, `str` may make -sense; typically to aid compatibility when the return types aren't the same -between the two Python versions. Avoid using `unicode`: it doesn't exist in -Python 3. - -The reason this discrepancy exists is because `str` means different things -depending on the Python version. +Prefer to use `str`, though `Text` is also acceptable. Be consistent in using +one or the other. For code that deals with binary data, use `bytes`. For Python +2 compatible code that processes text data (`str` or `unicode` in Python 2, +`str` in Python 3), use `Text`. ```python -No: -def py2_code(x: str) -> unicode: +def deals_with_text_data_in_py3(x: str) -> str: ... -``` - -For code that deals with binary data, use `bytes`. - -```python def deals_with_binary_data(x: bytes) -> bytes: ... +def py2_compatible_text_data_processor(x: Text) -> Text: + ... ``` -For Python 2 compatible code that processes text data (`str` or `unicode` in -Python 2, `str` in Python 3), use `Text`. For Python 3 only code that process -text data, prefer `str`. +In some uncommon Python 2 compatibility cases, `str` may make sense instead of +`Text`, typically to aid compatibility when the return types aren't the same +between Python 2 and Python 3. Never use `unicode` as it doesn't exist in Python +3. The reason this discrepancy exists is because `str` means something different +in Python 2 than in Python 3. + +No: ```python -from typing import Text -... -def py2_compatible(x: Text) -> Text: - ... -def py3_only(x: str) -> str: +def py2_code(x: str) -> unicode: ... ``` @@ -3385,18 +3297,16 @@ type. ```python from typing import Text, Union ... -def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: - ... def py3_only(x: Union[bytes, str]) -> Union[bytes, str]: ... +def py2_compatible(x: Union[bytes, Text]) -> Union[bytes, Text]: + ... ``` If all the string types of a function are always the same, for example if the return type is the same as the argument type in the code above, use [AnyStr](#typing-type-var). -Writing it like this will simplify the process of porting the code to Python 3. -