Skip to content

Commit

Permalink
[8ee3431] λ²ˆμ—­ μš”μ²­ πŸ’¬ (#37)
Browse files Browse the repository at this point in the history
* merge 8ee3431 πŸ›°

* [8ee3431] 1.1 배경 반영

* [8ee3431] 2.1 Lint 반영

* [8ee3431] 2.2 import 반영

* [8ee3431] 2.3 package 반영

* [8ee3431] 2.4 μ˜ˆμ™Έμ²˜λ¦¬ 반영

* [8ee3431] 2.5 이름 λ³€κ²½

* [8ee3431] 2.5 λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœ 반영

* [8ee3431] 2.6 쀑첩 반영

* [8ee3431] 2.7 list comprehensions

* [8ee3431] 2.8 κΈ°λ³Έ λ°˜λ³΅μžμ™€ μ—°μ‚°μž 반영

* [8ee3431] 2.9 μ œλ„ˆλ ˆμ΄ν„° 반영

* [8ee3431] 2.10 λžŒλ‹€ 반영

* [8ee3431] 2.12 κΈ°λ³Έ 인자 κ°’ 반영

* [8ee3431] 2.14 True/False 평가 반영

* [8ee3431] 2.16 λ ‰μ‹œμ»¬ μŠ€μ½”ν•‘ 반영

* [8ee3431] 2.17 ν•¨μˆ˜μ™€ λ©”μ„œλ“œ 반영

* [8ee3431] 2.18 μŠ€λ ˆλ“œ 반영

* [8ee3431] 2.20 ν˜„λŒ€μ˜ 파이썬 반영

* [8ee3431] 2.20 이름 λ³€κ²½

* [8ee3431] 2.21 Type 주석 반영

* [8ee3431] 3.2 쀄 길이 반영

* [8ee3431] 3.4 λ“€μ—¬μ“°κΈ° 반영

* [8ee3431] 3.5 빈 쀄 반영

* [8ee3431] 3.7 Shebang_Line 반영

* [8ee3431] 3.8 주석과 docstring 반영

* [8ee3431] 3.10 λ¬Έμžμ—΄ 반영

* [8ee3431] 3.12 TODO 주석 반영

* [8ee3431] 3.13 import ν˜•μ‹ 반영

* [8ee3431] 3.16 넀이밍 반영

* [8ee3431] 3.19 Type 주석 방법 반영

* [8ee3431] 4.1 맺음말 반영

* λΉŒλ“œ κ²½κ³  μˆ˜μ •

* λΉŒλ“œ κ²½κ³  μˆ˜μ •

* [8ee3431] build
  • Loading branch information
Sotaneum authored Sep 10, 2024
1 parent 3f40f2f commit 39284cc
Show file tree
Hide file tree
Showing 37 changed files with 2,329 additions and 1,444 deletions.
900 changes: 559 additions & 341 deletions Google Python Style Guide kor.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
- 이 μŠ€νƒ€μΌ κ°€μ΄λ“œλŠ” Python ν”„λ‘œκ·Έλž¨μ—μ„œ _ν•΄μ•Ό ν•  것듀과 ν•˜μ§€ 말아야 ν•  것_ 듀을 μ μ–΄λ†“μ•˜μŠ΅λ‹ˆλ‹€.
- μ½”λ“œ ν˜•μ‹μ„ μ •ν™•ν•˜κ²Œ ν•˜λŠ” 것듀 돕기 μœ„ν•΄, μš°λ¦¬λŠ” [settings file for Vim](google_python_style.vim)을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
- Emacs νŽΈμ§‘κΈ°μ—μ„œλŠ” κΈ°λ³Έ μ„€μ •κ°’μœΌλ‘œ μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.
- λ§Žμ€ νŒ€μ—μ„œ ν˜•μ‹μ— λŒ€ν•œ λ…ΌμŸμ„ ν”Όν•˜κΈ° μœ„ν•΄ [yapf](https://github.com/google/yapf/) auto-formatter 을 μ‚¬μš©ν•©λ‹ˆλ‹€.
- λ§Žμ€ νŒ€μ—μ„œ ν˜•μ‹μ— λŒ€ν•œ λ…ΌμŸμ„ ν”Όν•˜κΈ° μœ„ν•΄ [Black](https://github.com/psf/black) ν˜Ήμ€ [Pyink](https://github.com/google/pyink) auto-formatter 을 μ‚¬μš©ν•©λ‹ˆλ‹€.
2 changes: 1 addition & 1 deletion Google Python Style Guide/1. λ°°κ²½/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
- 이 μŠ€νƒ€μΌ κ°€μ΄λ“œλŠ” Python ν”„λ‘œκ·Έλž¨μ—μ„œ _ν•΄μ•Ό ν•  것듀과 ν•˜μ§€ 말아야 ν•  것_ 듀을 μ μ–΄λ†“μ•˜μŠ΅λ‹ˆλ‹€.
- μ½”λ“œ ν˜•μ‹μ„ μ •ν™•ν•˜κ²Œ ν•˜λŠ” 것듀 돕기 μœ„ν•΄, μš°λ¦¬λŠ” [settings file for Vim](google_python_style.vim)을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
- Emacs νŽΈμ§‘κΈ°μ—μ„œλŠ” κΈ°λ³Έ μ„€μ •κ°’μœΌλ‘œ μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.
- λ§Žμ€ νŒ€μ—μ„œ ν˜•μ‹μ— λŒ€ν•œ λ…ΌμŸμ„ ν”Όν•˜κΈ° μœ„ν•΄ [yapf](https://github.com/google/yapf/) auto-formatter 을 μ‚¬μš©ν•©λ‹ˆλ‹€.
- λ§Žμ€ νŒ€μ—μ„œ ν˜•μ‹μ— λŒ€ν•œ λ…ΌμŸμ„ ν”Όν•˜κΈ° μœ„ν•΄ [Black](https://github.com/psf/black) ν˜Ήμ€ [Pyink](https://github.com/google/pyink) auto-formatter 을 μ‚¬μš©ν•©λ‹ˆλ‹€.

---
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
- κ²½κ³ λ₯Ό λ„μš°μ§€ μ•ŠκΈ° μœ„ν•΄, μ½”λ“œμ— 라인 λ‹¨μœ„λ‘œ 주석을 달아야 ν•©λ‹ˆλ‹€.

```python
dict = 'something awful' # Bad Idea... pylint: disable=redefined-builtin
def do_PUT(self): # WSGI name, so pylint: disable=invalid-name
...
```

- `pylint`의 κ²½κ³ λŠ” 각각 symbolic name(`empty-docstring`)으둜 κ΅¬λ³„λ©λ‹ˆλ‹€.
Expand All @@ -54,15 +55,15 @@
- 각 λ©”μ‹œμ§€μ— λŒ€ν•΄ μžμ„Έν•œ 정보λ₯Ό μ–»κ³ μž ν•˜λŠ” 경우 λ‹€μŒκ³Ό 같은 λ°©λ²•μœΌλ‘œ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

```shell
pylint --help-msg=C6409
pylint --help-msg=invalid-name
```

- `pyling: disable-msg`λŠ” 이전에 μ‚¬μš©ν–ˆλ˜ λ°©μ‹μœΌλ‘œ μ΄μ œλŠ” μ‚¬μš©λ˜μ§€ μ•ŠμœΌλ©° `pylint: disable`λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.
- μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μΈμžμ— λŒ€ν•œ κ²½κ³ λŠ” ν•¨μˆ˜λ₯Ό μ‹œμž‘ν•  λ•Œ κ·Έ λ³€μˆ˜λ₯Ό μ§€μ›€μœΌλ‘œμ¨ μ–΅μ œν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‹€λ§Œ κ·Έ λ³€μˆ˜λ₯Ό μ™œ μ§€μ› λŠ”μ§€μ— λŒ€ν•΄ 항상 μ£Όμ„μœΌλ‘œ μ„€λͺ…을 μΆ”κ°€ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ κ²½μš°λŠ” "Unused."라고 μž‘μ„±ν•˜λ©΄ μΆ©λΆ„ν•©λ‹ˆλ‹€.
- μ•„λž˜ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•˜μ„Έμš”.

```python
def viking_cafe_order(spam: str, beans: str, eggs: Optional[str] = None) -> str:
def viking_cafe_order(spam: str, beans: str, eggs: str | None = None) -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#### 2.10.4 κ²°λ‘ 

- λžŒλ‹€λ₯Ό ν•œ μ€„λ‘œ μ‚¬μš©ν•˜μ„Έμš”. λ§Œμ•½ μ½”λ“œ 내뢀에 μžˆλŠ” λžŒλ‹€ ν•¨μˆ˜κ°€ 60~80κΈ€μž 수 μ •λ„λ‘œ κΈΈλ‹€λ©΄ 그건 μ•„λ§ˆ 더 일반적인
[Lexical Scoping(λ ‰μ‹œμ»¬ μŠ€μ½”ν•‘)](#s2.16-lexical-scoping)으둜 μ •μ˜ν•˜λŠ”κ²Œ λ‚˜μ„ κ²ƒμž…λ‹ˆλ‹€.

- λžŒλ‹€ ν•¨μˆ˜λŠ” ν—ˆμš©ν•˜μ§€λ§Œ, λžŒλ‹€ ν•¨μˆ˜ λ‚΄λΆ€μ˜ μ½”λ“œκ°€ μ—¬λŸ¬ 쀄에 κ±ΈμΉ˜κ±°λ‚˜ 60-80자λ₯Ό λ„˜λŠ” 경우, 일반적인 [쀑첩 ν•¨μˆ˜](#s2.16-lexical-scoping)둜 μ •μ˜ν•˜λŠ” 것이 더 λ‚˜μ„ 수 μžˆμŠ΅λ‹ˆλ‹€.
- κ³±μ…ˆ 같은 일반 μ—°μ‚°μžμ—μ„œλŠ” `operator`λͺ¨λ“ˆ λŒ€μ‹ μ— λžŒλ‹€ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- 예λ₯Ό λ“€μ–΄, `operator.mul`을 `lambda x,y : x * y` 처럼 μ‚¬μš©ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
def foo(a, b=None):
if b is None:
b = []
def foo(a, b: Optional[Sequence] = None):
def foo(a, b: Sequence | None = None):
if b is None:
b = []
def foo(a, b: Sequence = ()): # tuples은 λΆˆλ³€ν•˜κΈ° λ•Œλ¬Έμ— μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€.
Expand All @@ -60,12 +60,15 @@
- λΆ€μ μ ˆν•œ 예

```python
from absl import flags
_FOO = flags.DEFINE_string(...)

def foo(a, b=[]):
...
def foo(a, b=time.time()): # λͺ¨λ“ˆμ΄ λ‘œλ“œκ°€ 될 λ•Œμ˜ μ‹œκ°„μΈκ°€???
def foo(a, b=time.time()): # `b`κ°€ 이 λͺ¨λ“ˆμ΄ λ‘œλ“œλœ μ‹œκ°„μ„ λ‚˜νƒ€λ‚΄λŠ” κ²ƒμΈκ°€μš”?
...
def foo(a, b=FLAGS.my_thing): # sys.argvλŠ” 아직 ꡬ문 λΆ„μ„λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€...
def foo(a, b=_FOO.value): # sys.argvλŠ” 아직 ꡬ문 λΆ„μ„λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€...
...
def foo(a, b: Mapping = {}): # ν™•μΈλ˜μ§€ μ•Šμ€ μ½”λ“œλ‘œ 전달 될 수 μžˆμŠ΅λ‹ˆλ‹€...
def foo(a, b: Mapping = {}): # ν™•μΈλ˜μ§€ μ•Šμ€ μ½”λ“œλ‘œ 전달 될 수 μžˆμŠ΅λ‹ˆλ‹€.
...
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### 2.14 True/False 평가

- κ°€λŠ₯ν•œ 경우 "암묡적(implicit)" `false`λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- κ°€λŠ₯ν•œ 경우 "암묡적(implicit)" `false`λ₯Ό μ‚¬μš©ν•˜μ„Έμš”. (λͺ‡ 가지 μ£Όμ˜μ‚¬ν•­μ΄ μžˆμ§€λ§Œ)

<a id="s2.14.1-definition"></a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#### 2.16.3 단점

- ν˜Όλž€μŠ€λŸ¬μš΄ λ²„κ·Έλ‘œ μ΄μ–΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. [PEP-0227](http://www.google.com/url?sa=D&q=http://www.python.org/dev/peps/pep-0227/)μ—μ„œ μžμ„Έν•œ 정보λ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.
- [PEP-0227](https://peps.python.org/pep-0227)을 기반으둜 ν•œ 이 μ˜ˆμ‹œμ™€ 같이 ν˜Όλž€μŠ€λŸ¬μš΄ 버그λ₯Ό μ΄ˆλž˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

```python
i = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@

- DecoratorλŠ” λΆ„λͺ…ν•œ 이점이 μžˆλ”λΌλ„ ν˜„λͺ…ν•˜κ²Œ μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€. DecoratorλŠ” import와 λͺ…λͺ… 지침을 따라야 ν•©λ‹ˆλ‹€. Decorator pydocλŠ” decorator ν•¨μˆ˜ μž„μ„ λΆ„λͺ…νžˆ λͺ…μ‹œν•΄μ•Όν•©λ‹ˆλ‹€. dcoratorλ₯Ό μœ„ν•œ μœ λ‹› ν…ŒμŠ€νŠΈ(unit test)λ₯Ό μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.
- Decorator(예. 파일, μ†ŒμΌ“, λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° λ“±) λ₯Ό μ‹€ν–‰ν•  λ•Œ (`pydoc` ν˜Ήμ€ 기타 도ꡬλ₯Ό import μ‹œκ°„μ— κ°€μ Έμ˜¬ λ•Œ) μ‚¬μš© λͺ»ν•  수 μžˆμœΌλ―€λ‘œ Decorator의 μ™ΈλΆ€ μ˜μ‘΄μ„±μ„ ν”Όν•˜μ„Έμš”. μœ νš¨ν•œ λ§€κ°œλ³€μˆ˜λ₯Ό 가진 Decorator은 λͺ¨λ“  κ²½μš°μ— μž‘λ™ν•  수 μžˆλ„λ‘ 보μž₯λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.
- DecoratorλŠ” "Top level code"의 νŠΉλ³„ν•œ 경우일 λ•Œμ—λŠ” [main](#s3.17-main) ν•­λͺ©μ— μžμ„Έν•œ λ‚΄μš©μ΄ μžˆμŠ΅λ‹ˆλ‹€.
- DecoratorλŠ” "Top-level code"의 νŠΉλ³„ν•œ 경우일 λ•Œμ—λŠ” [main](#s3.17-main) ν•­λͺ©μ— μžμ„Έν•œ λ‚΄μš©μ΄ μžˆμŠ΅λ‹ˆλ‹€.
- κΈ°μ‘΄ λΌμ΄λΈŒλŸ¬λ¦¬μ— μ •μ˜λœ API와 ν†΅ν•©ν•˜κΈ° μœ„ν•΄ κ°•μ œν•˜μ§€ μ•ŠλŠ” ν•œ "staticmethod"λ₯Ό μ‚¬μš©ν•˜μ§€ λ§ˆμ„Έμš”. λŒ€μ‹  λͺ¨λ“ˆ 레벨 ν•¨μˆ˜λ₯Ό μ“°μ„Έμš”.
- ν”„λ‘œμ„ΈμŠ€ 전체 μΊμ‹œ λ“± ν•„μš”ν•œ global stateλ₯Ό μˆ˜μ •ν•˜λŠ” λͺ…λͺ…λœ μƒμ„±μž λ˜λŠ” ν΄λž˜μŠ€λ³„ 루틴을 μž‘μ„±ν•  λ•Œλ§Œ "classmethod"λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- classmethodλŠ” λͺ…λͺ…λœ μƒμ„±μžλ₯Ό μž‘μ„±ν•˜κ±°λ‚˜ ν”„λ‘œμ„ΈμŠ€ 전체 μΊμ‹œμ™€ 같은 ν•„μˆ˜ μ „μ—­ μƒνƒœλ₯Ό μˆ˜μ •ν•˜λŠ” 클래슀 νŠΉμ • 루틴을 μž‘μ„±ν•  λ•Œλ§Œ μ‚¬μš©ν•˜μ„Έμš”.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

### 2.18 μŠ€λ ˆλ“œ

- λ‚΄μž₯된 νƒ€μž…μ˜ μ›μžμ„±μ— μ˜μ‘΄ν•˜μ§€ λ§ˆμ„Έμš”. λ”•μ…”λ„ˆλ¦¬μ™€ 같은 Python의 λ‚΄μž₯된 νƒ€μž…μ€ μ›μž ν˜•νƒœλ‘œ μ‘°μž‘ν•  수 μžˆμ§€λ§Œ κ·ΈλŸ¬μ§€ μ•Šμ€ 경우(예: `__hash__`μ΄λ‚˜ `__eq__`κ°€ Python ν•¨μˆ˜λ‘œ κ΅¬ν˜„λ˜λŠ” 경우)도 있으며 μ›μžλ‘œ λ˜μ–΄μžˆλ‹€κ³  μ‹ λ’°ν•˜λ©΄ μ•ˆ λ©λ‹ˆλ‹€. λ˜ν•œ, μ›μž λ³€μˆ˜ 할당에 μ˜μ‘΄ν•΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€. (κ²°κ΅­, λ”•μ…”λ„ˆλ¦¬μ— λ‹¬λ €μžˆκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€) μŠ€λ ˆλ“œ κ°„ 데이터λ₯Ό ν†΅μ‹ ν•˜λŠ” 데 μ„ ν˜Έν•˜λŠ” λ°©λ²•μœΌλ‘œ 큐 λͺ¨λ“ˆμ˜ `Queue` 데이터 νƒ€μž…μ„ μ‚¬μš©ν•˜μ„Έμš”. 그렇지 μ•ŠμœΌλ©΄ threading λͺ¨λ“ˆμ΄λ‚˜ locking primitivesλ₯Ό μ‚¬μš©ν•˜μ„Έμš”. lower-level lock λŒ€μ‹ ν•΄ Condition variables와 `threading.Condition`λ₯Ό μ„ ν˜Έν•˜μ„Έμš”.
- λ‚΄μž₯된 νƒ€μž…μ˜ μ›μžμ„±μ— μ˜μ‘΄ν•˜μ§€ λ§ˆμ„Έμš”. λ”•μ…”λ„ˆλ¦¬μ™€ 같은 Python의 λ‚΄μž₯된 νƒ€μž…μ€ μ›μž ν˜•νƒœλ‘œ μ‘°μž‘ν•  수 μžˆμ§€λ§Œ κ·ΈλŸ¬μ§€ μ•Šμ€ 경우(예: `__hash__`μ΄λ‚˜ `__eq__`κ°€ Python ν•¨μˆ˜λ‘œ κ΅¬ν˜„λ˜λŠ” 경우)도 있으며 μ›μžλ‘œ λ˜μ–΄μžˆλ‹€κ³  μ‹ λ’°ν•˜λ©΄ μ•ˆ λ©λ‹ˆλ‹€. λ˜ν•œ, μ›μž λ³€μˆ˜ 할당에 μ˜μ‘΄ν•΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€. (κ²°κ΅­, λ”•μ…”λ„ˆλ¦¬μ— λ‹¬λ €μžˆκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€) μŠ€λ ˆλ“œ κ°„ 데이터λ₯Ό ν†΅μ‹ ν•˜λŠ” 데 μ„ ν˜Έν•˜λŠ” λ°©λ²•μœΌλ‘œ `queue` λͺ¨λ“ˆμ˜ `Queue` 데이터 νƒ€μž…μ„ μ‚¬μš©ν•˜μ„Έμš”. 그렇지 μ•ŠμœΌλ©΄ `threading` λͺ¨λ“ˆμ΄λ‚˜ locking primitivesλ₯Ό μ‚¬μš©ν•˜μ„Έμš”. lower-level lock λŒ€μ‹ ν•΄ Condition variables와 `threading.Condition`λ₯Ό μ„ ν˜Έν•˜μ„Έμš”.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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`문은 κ°œλ³„ νƒ€μž…, 클래슀, ν•¨μˆ˜κ°€ μ•„λ‹ˆλΌ νŒ¨ν‚€μ§€μ™€ λͺ¨λ“ˆμ—λ§Œ μ‚¬μš©ν•˜μ„Έμš”. (μ£Ό: κ°œλ³„ νƒ€μž…μ€ κΈ°λ³Έ μžλ£Œν˜•, μ‚¬μš©μž μ •μ˜ 클래슀λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.)

<a id="s2.2.1-definition"></a>

Expand All @@ -28,8 +28,13 @@

- `import x`λ₯Ό νŒ¨ν‚€μ§€μ™€ λͺ¨λ“ˆμ„ importν• λ•Œ μ‚¬μš©ν•˜μ„Έμš”.
- `from x import y`λ₯Ό `x`κ°€ νŒ¨ν‚€μ§€μ˜ 접두어이고 `y`κ°€ 접두어가 μ—†λŠ” λͺ¨λ“ˆμΌλ•Œ μ‚¬μš©ν•˜μ„Έμš”.
- λ§Œμ•½ `y` 둜 이름이 지어진 두 λͺ¨λ“ˆμ΄ importλ˜κ±°λ‚˜ `y` κ°€ λΆˆν•„μš”ν•˜κ²Œ λ„ˆλ¬΄ κΈ΄ 이름을 κ°€μ‘Œλ‹€λ©΄ `from x import y as z`λ₯Ό μ‚¬μš©μ„Έμš”.
- `import y as z`λ₯Ό `z` κ°€ 곡식적인 약어인 κ²½μš°μ—λ§Œ μ‚¬μš©ν•˜μ„Έμš”(e.g., `np` λŠ” `numpy` λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.)
- λ‹€μŒκ³Ό 같은 μƒν™©μ—μ„œλŠ” `from x import y as z` λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- `y`λΌλŠ” 이름을 가진 두 개의 λͺ¨λ“ˆμ„ κ°€μ Έμ˜΅λ‹ˆλ‹€.
- `y`λŠ” ν˜„μž¬ λͺ¨λ“ˆμ— μ •μ˜λœ μ΅œμƒμœ„ 이름과 μΆ©λŒν•©λ‹ˆλ‹€.
- `y`λŠ” 곡개 API의 일뢀인 곡톡 λ§€κ°œλ³€μˆ˜ 이름(예: 'features')κ³Ό μΆ©λŒν•©λ‹ˆλ‹€.
- `y`λŠ” λΆˆνŽΈν•  μ •λ„λ‘œ κΈ΄ μ΄λ¦„μž…λ‹ˆλ‹€.
- `y`λŠ” μ½”λ“œ μ»¨ν…μŠ€νŠΈμ—μ„œ λ„ˆλ¬΄ μΌλ°˜μ μž…λ‹ˆλ‹€(예: `from storage.file_system import options as fs_options`).
- `import y as z`λ₯Ό `z` κ°€ 곡식적인 약어인 κ²½μš°μ—λ§Œ μ‚¬μš©ν•˜μ„Έμš”(e.g., `import numpy as np`)

- 예λ₯Όλ“€μ–΄ `sound.effects.echo`λͺ¨λ“ˆμ΄ import λœλ‹€λ©΄ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

Expand All @@ -42,3 +47,14 @@
- importλœκ²ƒλ“€κ³Ό κ΄€λ ¨μžˆλŠ” 이름을 μ‚¬μš©ν•˜μ§€λ§ˆμ„Έμš”.
- λͺ¨λ“ˆμ΄ 같은 νŒ¨ν‚€μ§€μ— μžˆλ”λΌλ„ 전체 νŒ¨ν‚€μ§€ 이름을 μ‚¬μš©ν•˜μ„Έμš”.
- μ΄λŠ” 무심코 νŒ¨ν‚€μ§€λ₯Ό λ‘λ²ˆ import ν•˜λŠ”κ²ƒμ„ μ˜ˆλ°©ν•˜λŠ” 것에 도움이 λ©λ‹ˆλ‹€.

<a id="imports-exemptions"></a>

##### 2.2.4.1 μ˜ˆμ™Έ

- 이 κ·œμΉ™μ˜ μ˜ˆμ™ΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
- λ‹€μŒ λͺ¨λ“ˆμ˜ 심볼은 정적 뢄석 및 νƒ€μž… 검사λ₯Ό μ§€μ›ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`typing` module](#typing-imports)
- [`collections.abc` module](#typing-imports)
- [`typing_extensions` module](https://github.com/python/typing_extensions/blob/main/README.md)
- [six.moves module](https://six.readthedocs.io/#module-six.moves)의 λ¦¬λ‹€μ΄λ ‰μ…˜μž…λ‹ˆλ‹€.
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,4 @@
from __future__ import generator_stop
```

- 2.7 버전을 계속 μ§€μ›ν•΄μ•Όν•˜λŠ” 뢀담이 μžˆλŠ” λ ˆκ±°μ‹œ μ½”λ“œμ˜ 경우

```python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
```

- μžμ„Έν•œ λ‚΄μš©μ€ [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 λͺ¨λ‘μ—μ„œ μ‚¬μš©μ„ μ§€μ›ν•΄μ•Όν•˜λŠ” 경우 μ ν•©ν•˜λ‹€κ³  νŒλ‹¨λ˜λŠ” λŒ€λ‘œ [six](https://pypi.org/project/six/), [future](https://pypi.org/project/future/), 및 [past](https://pypi.org/project/past/) 라이브러리λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- μ½”λ“œλ₯Ό 더 κΉ¨λ—ν•˜κ³  삢을 더 μ‰½κ²Œ λ§Œλ“€κΈ° μœ„ν•΄ μ‘΄μž¬ν•©λ‹ˆλ‹€.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

### 2.21 Type 주석

- Python 3μ—μ„œ νƒ€μž…μ˜ 정보λ₯Ό [PEP-484](https://www.python.org/dev/peps/pep-0484/)의 μ°Έκ³ ν•΄μ„œ μ£Όμ„μœΌλ‘œ 달 수 μžˆμŠ΅λ‹ˆλ‹€. 그리고 λΉŒλ“œ ν•  λ•Œ [pytype](https://github.com/google/pytype)같은 νƒ€μž…κ²€μ‚¬λ„κ΅¬λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- Type에 λŒ€ν•œ 주석은 μ†ŒμŠ€ μ•ˆμ΄λ‚˜ [stub pyi 파일](https://www.python.org/dev/peps/pep-0484/#stub-files)에 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. κ°€λŠ₯ν•˜λ©΄ 주석은 μ†ŒμŠ€μ•ˆμ— μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 타사 λ˜λŠ” ν™•μž₯ λͺ¨λ“ˆμ—λŠ” pyi νŒŒμΌμ„ μ‚¬μš©ν•˜μ„Έμš”.
- Pythonμ—μ„œ νƒ€μž…μ˜ 정보λ₯Ό [PEP-484](https://www.python.org/dev/peps/pep-0484/)의 μ°Έκ³ ν•΄μ„œ μ£Όμ„μœΌλ‘œ 달 수 μžˆμŠ΅λ‹ˆλ‹€. 그리고 λΉŒλ“œ ν•  λ•Œ [pytype](https://github.com/google/pytype)같은 νƒ€μž…κ²€μ‚¬λ„κ΅¬λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- Type에 λŒ€ν•œ 주석은 μ†ŒμŠ€ μ•ˆμ΄λ‚˜ [stub pyi 파일](https://peps.python.org/pep-0484/#stub-files)에 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. κ°€λŠ₯ν•˜λ©΄ 주석은 μ†ŒμŠ€μ•ˆμ— μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 타사 λ˜λŠ” ν™•μž₯ λͺ¨λ“ˆμ—λŠ” pyi νŒŒμΌμ„ μ‚¬μš©ν•˜μ„Έμš”.

<a id="s2.21.1-definition"></a>

Expand All @@ -13,21 +13,15 @@
- Type의 주석(ν˜Ήμ€ Type 정보)은 ν•¨μˆ˜λ‚˜ λ©”μ„œλ“œμ˜ μΈμžκ°’μ΄λ‚˜ λ°˜ν™˜κ°’μž…λ‹ˆλ‹€

```python
def func(a: int) -> List[int]:
def func(a: int) -> list[int]:
```

- [PEP-526](https://www.python.org/dev/peps/pep-0526/)ꡬ문 처럼 λ³€μˆ˜μ˜ type을 μ„ μ–Έν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
- [PEP-526](https://peps.python.org/pep-0526/)ꡬ문 처럼 λ³€μˆ˜μ˜ type을 μ„ μ–Έν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

```python
a: SomeType = some_func()
```

- legacy Python version을 μ§€μ›ν•΄μ•Όν•œλ‹€λ©΄ μ½”λ“œμ— type μ„€λͺ…을 μΆ”κ°€ν•©λ‹ˆλ‹€.

```python
a = some_func() # type: SomeType
```

<a id="s2.21.2-pros"></a>

#### 2.21.2 μž₯점
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
import absl.flags
from doctor.who import jodie

FLAGS = absl.flags.FLAGS
_FOO = absl.flags.DEFINE_string(...)
```

```python
# Reference flags in code with just the module name (common).
from absl import flags
from doctor.who import jodie

FLAGS = flags.FLAGS
_FOO = flags.DEFINE_string(...)
```

- λΆ€μ μ ˆν•œ 예 _(이 νŒŒμΌμ€ `doctor/who/` 에 μžˆλ‹€κ³  κ°€μ •ν•˜κ³  `jodie.py`λ˜ν•œ μ‘΄μž¬ν•œλ‹€κ³  κ°€μ •ν•©λ‹ˆλ‹€.)_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@

##### μ˜ˆμ™ΈλŠ” λ‹€μŒκ³Ό 같은 쑰건을 λ§Œμ‘±ν•΄μ•Ό ν•©λ‹ˆλ‹€

- μ μ ˆν•œ 경우 λ‚΄μž₯ μ˜ˆμ™Έ 클래슀λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
- 예λ₯Ό λ“€μ–΄, λ§Œμ•½ μ–‘μˆ˜λ₯Ό μ˜ˆμƒν•˜λŠ”λ° μŒμˆ˜κ°€ ν†΅κ³Όν•œλ‹€λ©΄ `ValueError`λ₯Ό λ°œμƒμ‹œλŠ” 것이 κ·Έ μ˜ˆμž…λ‹ˆλ‹€.
- 곡곡 API에 μžˆλŠ” 인수의 값을 κ²€μ¦ν•˜κΈ° μœ„ν•΄ `assert`문을 μ‚¬μš©ν•˜μ§€λ§ˆμ„Έμš”.
- `assert`λŠ” μ˜¬λ°”λ₯Έ μ‚¬μš©μ΄λ‚˜ μ˜ˆμƒμΉ˜ λͺ»ν•œ 이벀트 λ°œμƒμ„ λ‚˜νƒ€λ‚΄λŠ” 것이 μ•„λ‹ˆλΌ 내뢀적 정확성을 보μž₯ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ©λ‹ˆλ‹€. λ§Œμ•½ λ‚˜μ€‘μ— μ˜ˆμ™Έκ°€ ν•„μš”ν•˜λ‹€λ©΄, raise문을 μ‹€ν–‰ν•˜μ„Έμš”.
- λ‚΄μž₯ μ˜ˆμ™Έ 클래슀λ₯Ό μ‚¬μš©ν•˜λŠ” 데에 λ¬Έμ œκ°€ μ—†λ‹€λ©΄ μ‚¬μš©ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, ν•¨μˆ˜ 인자λ₯Ό 검증할 λ•Œμ™€ 같은 ν”„λ‘œκ·Έλž˜λ° 였λ₯˜λ‚˜ μœ„λ°˜λœ μ „μ œμ‘°κ±΄μ„ λ‚˜νƒ€λ‚΄κΈ° μœ„ν•΄ `ValueError`λ₯Ό λ°œμƒμ‹œν‚€μ„Έμš”.
- `assert` 문을 μ‘°κ±΄λ¬Έμ΄λ‚˜ μ „μ œμ‘°κ±΄ 검증 λŒ€μ‹  μ‚¬μš©ν•˜μ§€ λ§ˆμ„Έμš”. application logic에 μ€‘μš”ν•˜μ§€ μ•Šμ•„μ•Ό ν•©λ‹ˆλ‹€. `assert`문을 μ œκ±°ν•΄λ„ μ½”λ“œκ°€ μ •μƒμ μœΌλ‘œ μž‘λ™ν•œλ‹€λ©΄, 그것이 기쀀이 될 수 μžˆμŠ΅λ‹ˆλ‹€. assert 쑰건문은 평가될 것이라고 [보μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€](https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement). [pytest](https://pytest.org)기반의 ν…ŒμŠ€νŠΈμ—μ„œλŠ” `assert` 문을 μ‚¬μš©ν•˜μ—¬ κΈ°λŒ€κ°’μ„ κ²€μ¦ν•˜λŠ” 것이 μ μ ˆν•˜κ³  μ˜ˆμƒλ©λ‹ˆλ‹€

- μ˜¬λ°”λ₯Έ 예

Expand All @@ -52,9 +50,10 @@
if minimum < 1024:
raise ValueError(f'Min. port must be at least 1024, not {minimum}.')
port = self._find_next_open_port(minimum)
if not port:
if port is None:
raise ConnectionError(
f'Could not connect to service on port {minimum} or higher.')
# 이 μ½”λ“œμ˜ κ²°κ³ΌλŠ” 이 assert에 μ˜μ‘΄ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
assert port >= minimum, (
f'Unexpected port {port} when minimum was {minimum}.')
return port
Expand All @@ -72,8 +71,10 @@
The new minimum port.
"""
assert minimum >= 1024, 'Minimum port must be at least 1024.'
# 이 μ½”λ“œλŠ” 이전 assert에 μ˜μ‘΄ν•©λ‹ˆλ‹€.
port = self._find_next_open_port(minimum)
assert port is not None
# λ°˜ν™˜ 값에 λŒ€ν•œ νƒ€μž… κ²€μ‚¬λŠ” assert에 μ˜μ‘΄ν•©λ‹ˆλ‹€.
return port
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<a id="s2.5-global-variables"></a>
<a id="25-global-variables"></a>
<a id="s2.5-global-state"></a>
<a id="25-global-state"></a>

### 2.5 λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœ

- λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœλ₯Ό μ‚¬μš©ν•˜μ§€ λ§ˆμ„Έμš”.

<a id="s2.5.1-definition"></a>

#### 2.5.1 μ •μ˜

- ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 쀑 변경될 수 μžˆλŠ” λͺ¨λ“ˆ μˆ˜μ€€μ˜ κ°’μ΄λ‚˜ 클래슀 속성을 λ§ν•©λ‹ˆλ‹€.

<a id="s2.5.2-pros"></a>

#### 2.5.2 μž₯점

- 가끔 νŽΈλ¦¬ν•©λ‹ˆλ‹€.

<a id="s2.5.3-cons"></a>
<a id="253-cons"></a>
<a id="global-variables-cons"></a>

#### 2.5.3 단점

- μΊ‘μŠν™”κ°€ κΉ¨μ§‘λ‹ˆλ‹€.
- μ΄λŸ¬ν•œ μ„€κ³„λŠ” μœ νš¨ν•œ λͺ©ν‘œλ₯Ό λ‹¬μ„±ν•˜κΈ° μ–΄λ ΅κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄, μ „μ—­ μƒνƒœλ₯Ό μ‚¬μš©ν•˜μ—¬ λ°μ΄ν„°λ² μ΄μŠ€ 연결을 κ΄€λ¦¬ν•˜λŠ” 경우, 두 개의 μ„œλ‘œ λ‹€λ₯Έ λ°μ΄ν„°λ² μ΄μŠ€λ₯Ό λ™μ‹œμ— μ—°κ²°ν•˜λŠ” 것이 μ–΄λ €μ›Œμ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€(e.g., λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ 쀑 차이λ₯Ό 계산할 λ•Œ). μ „μ—­ λ ˆμ§€μŠ€νŠΈλ¦¬μ—μ„œλ„ μœ μ‚¬ν•œ λ¬Έμ œκ°€ μ‰½κ²Œ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
- λͺ¨λ“ˆμ΄ 처음 μž„ν¬νŠΈλ  λ•Œ μ „μ—­ λ³€μˆ˜μ— λŒ€ν•œ 할당이 이루어지기 λ•Œλ¬Έμ—, λͺ¨λ“ˆμ˜ λ™μž‘μ„ μž„ν¬νŠΈ 쀑에 λ³€κ²½ν•  κ°€λŠ₯성이 μžˆμŠ΅λ‹ˆλ‹€.

<a id="s2.5.4-decision"></a>
<a id="254-decision"></a>
<a id="global-variables-decision"></a>

#### 2.5.4 κ²°λ‘ 

- λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœλ₯Ό μ‚¬μš©ν•˜μ§€ λ§ˆμ„Έμš”.
- μ „μ—­ μƒνƒœλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 λ“œλ¬Όκ²Œ ν•„μš”ν•œ 경우, λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μ—”ν‹°ν‹°λŠ” λͺ¨λ“ˆ μˆ˜μ€€μ΄λ‚˜ 클래슀 μ†μ„±μœΌλ‘œ μ„ μ–Έν•˜κ³ , 이름 μ•žμ— `_`λ₯Ό λΆ™μ—¬ λ‚΄λΆ€μ μœΌλ‘œ μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€. ν•„μš”ν•œ 경우, λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœμ— λŒ€ν•œ μ™ΈλΆ€ 접근은 곡개 ν•¨μˆ˜λ‚˜ 클래슀 λ©”μ†Œλ“œλ₯Ό 톡해 이루어져야 ν•©λ‹ˆλ‹€. [Naming](#s3.16-naming)λ₯Ό μ°Έκ³ ν•˜μ„Έμš”. λ³€κ²½ κ°€λŠ₯ν•œ μ „μ—­ μƒνƒœλ₯Ό μ‚¬μš©ν•˜λŠ” 섀계 μ΄μœ μ— λŒ€ν•΄μ„œλŠ” 주석에 μ„€λͺ…ν•˜κ±°λ‚˜ 링크된 λ¬Έμ„œμ—μ„œ μ„€λͺ…ν•΄ μ£Όμ„Έμš”.
- λͺ¨λ“ˆ μˆ˜μ€€μ˜ μƒμˆ˜λŠ” ν—ˆμš©λ˜λ©° ꢌμž₯λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λ‚΄λΆ€ μš©λ„μ˜ μƒμˆ˜μ—λŠ” `_MAX_HOLY_HANDGRENADE_COUNT = 3`λ₯Ό μ‚¬μš©ν•˜κ³ , Public API μƒμˆ˜μ—λŠ” `SIR_LANCELOTS_FAVORITE_COLOR = "blue"`λ₯Ό μ‚¬μš©ν•˜μ„Έμš”. μƒμˆ˜λŠ” λͺ¨λ‘ λŒ€λ¬Έμžμ™€ 밑쀄을 μ‚¬μš©ν•˜μ—¬ λͺ…λͺ…ν•΄μ•Ό ν•©λ‹ˆλ‹€. [Naming](#s3.16-naming)λ₯Ό μ°Έκ³ ν•˜μ„Έμš”.
Loading

0 comments on commit 39284cc

Please sign in to comment.