Hi there πŸ‘‹

λ– μ˜€λ₯΄λŠ” 생각을 λŠ˜μ–΄λ†“μŠ΅λ‹ˆλ‹€.

default value parameter κ°€ 가독성에 λ―ΈμΉ˜λŠ” ν•΄μ•…

default value parameter λŠ” ν•¨μˆ˜μ— νŒŒλΌλ―Έν„°λ₯Ό 지정할 λ•Œ 자주 μ“°μ΄λŠ” 방법이닀. def foo(a: bool = True): if a: return "foo" else: return "bar" λ¬Έμ œλŠ” 이 방법이 ν•¨μˆ˜μ˜ μ œμž‘μžμ˜ κ²½μš°μ—λŠ” λͺ…μ‹œμ μœΌλ‘œ λŠκ»΄μ§€μ§€λ§Œ μ‚¬μš©μž ν˜Ήμ€ λ…μžμ˜ κ²½μš°μ—λŠ” ꡉμž₯히 λ¬΅μ‹œμ μ΄λΌλŠ”λ° μžˆλ‹€. # default argument >> foo() "foo" # positional argument >> foo(True) "foo" # keyword argument >> foo(a=False) "bar" default value parameter λŠ” positional / keyword argument 외에도 ν•˜λ‚˜μ˜ 방법을 더 λŠ˜λ €μ£ΌλŠ”λ°, λͺ…μ‹œμ μœΌλ‘œ argument λ₯Ό λ„˜κΈ°μ§€ μ•Šμ•„λ„ 이미 μ§€μ •λœ default value λ₯Ό ν•¨μˆ˜ λ‚΄μ—μ„œ μ‚¬μš©ν•  수 있게 끔 ν•œλ‹€....

December 18, 2023 Β· 2 min Β· Dongwook Kim

Mixin class μ‚¬μš©μ‹œ app κ°„ 계측 ꡬ쑰

μ•žμ„œ ν¬μŠ€νŒ…ν•œ circular import 에 λŒ€ν•œ 생각 μ—μ„œ ν•΄λ‹Ή λ¬Έμ œκ°€ λ°œμƒν•œ 경우, 계측 ꡬ쑰λ₯Ό λ¨Όμ € μƒκ°ν•΄λ³΄μžλŠ” 이야기λ₯Ό ν–ˆμ—ˆλ‹€. μ΄λ²ˆμ—λŠ” μ΄μ–΄μ„œ class 의 κ³΅ν†΅λœ κΈ°λŠ₯을 뽑아낼 λ•Œ 계측 ꡬ쑰λ₯Ό 잘 작기 μœ„ν•œ 방법을 μƒκ°ν•΄λ³΄μ•˜λ‹€. 일반적으둜 Mixin class λŠ” ν•˜μœ„ ν΄λž˜μŠ€λ“€μ˜ 곡톡을 λ¬Άμ–΄ μƒμœ„ abstract 둜 λ½‘λŠ”λ° μ‚¬μš©λ˜λ‚˜, 가끔은 μƒμœ„ ν΄λž˜μŠ€λ“€μ— κ³΅ν†΅λœ κΈ°λŠ₯을 λΆ™μ΄λŠ” μš©λ„λ‘œλ„ μ‚¬μš©λœλ‹€. django 둜 κ°€μ •ν•˜κ³  μ˜ˆμ‹œλ₯Ό λ“ λ‹€. # A app class A(models.Model): nickname = models.CharField(...) def validate_nickname(self): ... # B app class B(models....

December 18, 2023 Β· 3 min Β· Dongwook Kim

Python λ‚΄λΆ€μ˜ C μ½”λ“œ 듀여닀보기

! μ•„λž˜μ˜ μ˜ˆμ œλŠ” λͺ¨λ‘ macOS Monterey 12.5.1 μ—μ„œ μž‘μ„±λ˜μ—ˆλ‹€. λ¨Όμ € python 내뢀에 c μ½”λ“œλ₯Ό 듀여닀보기 μœ„ν•΄μ„œλŠ” cpython μ†ŒμŠ€λ₯Ό λ°›μ•„μ•Ό ν•œλ‹€. git clone https://github.com/python/cpython cd cpython μ›ν•˜λŠ” λ²„μ „μœΌλ‘œ branch λ₯Ό λ°”κΏ”μ£Όκ³  μ»΄νŒŒμΌμ„ μ§„ν–‰ν•œλ‹€. configure μ˜΅μ…˜ μ°Έκ³  git switch 3.9 ./configure --with-pydebug --with-openssl=$(brew --prefix openssl) make -s -j2 make μ‹œ macOS μ—μ„œ 'lzma.h' file not found κ°€ λ°œμƒν•  경우 brew 둜 xz λ₯Ό μ„€μΉ˜ν•˜κ³  ν™˜κ²½λ³€μˆ˜λ₯Ό μž‘μ•„μ€€λ‹€. ν™˜κ²½λ§ˆλ‹€ pathκ°€ λ‹€λ₯Όμˆ˜ μžˆμŒμ— 주의...

October 21, 2022 Β· 4 min Β· Dongwook Kim

PEP 3119 ABC λ²ˆμ—­

PEP 3119 – Introducing Abstract Base Classes original link 이둠적 해석 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ, 객체와 μƒν˜Έμž‘μš©ν•˜λŠ” μ‚¬μš© νŒ¨ν„΄μ€ 두가지 κΈ°λ³Έ λΆ„λ₯˜λ‘œ λ‚˜λˆŒ 수 μžˆλ‹€. ν•˜λ‚˜λŠ” β€˜ν˜ΈμΆœ(invocation)’ 이고 λ‹€λ₯Έ ν•˜λ‚˜λŠ” β€˜λΆ„μ„(inspection)’ 이닀. ν˜ΈμΆœμ€ 객체의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” 것을 μ˜λ―Έν•œλ‹€. λŒ€κ°œ λ‹€ν˜•μ„±κ³Ό κ²°ν•©λ˜μ–΄ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μ–΄λ–€ νƒ€μž…μ˜ 객체이냐에 따라 λ‹€λ₯Έ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜κ²Œ λœλ‹€. class A: def foo(self): print("A") class B(A): def foo(self): print("B") A().foo() B().foo() 뢄석은 μ™ΈλΆ€ μ½”λ“œ(ν•΄λ‹Ή 객체의 λ©”μ†Œλ“œ λ°–μ˜)μ—μ„œ ν•΄λ‹Ή 객체의 νƒ€μž…μ΄λ‚˜ ν”„λ‘œνΌν‹°λ₯Ό ν™•μΈν•˜κ³  ν™•μΈν•œ 정보에 따라 μ–΄λ–»κ²Œ 객체λ₯Ό 닀룰지 κ²°μ •ν•˜λŠ” 것을 μ˜λ―Έν•œλ‹€....

September 23, 2022 Β· 3 min Β· Dongwook Kim

Circular import에 λŒ€ν•œ 생각

인터넷을 보면 λ§Žμ€ circular import 문제 ν•΄κ²° 방법을 찾을 수 μžˆλ‹€. λŒ€ν‘œμ μΈ 해결방법. def foo(): import smth ... μœ„μ™€ 같이 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ import λ₯Ό ν•œλ‹€λ©΄ νšŒν”Όκ°€ κ°€λŠ₯ν•˜λ‹€. ν”„λ‘œκ·Έλž¨μ€ 문제 없이 λ™μž‘ν•˜κΈ° μ‹œμž‘ν•˜κ³ , 이후 이 λ¬Έμ œλŠ” κ·ΈλŒ€λ‘œ μžŠν˜€μ§„λ‹€. 이게 μ˜³μ€ λ°©λ²•μΌκΉŒ? 일단 ν•΄λ‹Ή λ¬Έμ œκ°€ μ™œ λ°œμƒν•˜λŠ” 지뢀터 μƒκ°ν•΄λ³΄μž. # foo.py import bar ... # bar.py import foo ... μ΄λ ‡κ²Œ μ„œλ‘œλ₯Ό import ν•˜λŠ” 경우 λ°œμƒν•œλ‹€. κ°œλ°œμ„ ν•˜λ‹€λ³΄λ©΄ ν”ν•˜κ²Œ λ°œμƒν•˜λŠ” 경우인데, λ‹¨μˆœνžˆ μœ„μ— μ†Œκ°œν•œ λ°©λ²•μœΌλ‘œ νšŒν”Όν•˜λŠ” κ²ƒλ§Œ κ³ λ €ν•˜μ—¬ 아쉬움이 μžˆλ‹€....

February 21, 2021 Β· 2 min Β· Dongwook Kim

Early return에 λŒ€ν•œ 생각

early return μ΄λž€ νŠΉμ • μƒν™©μ—μ„œ function λ‚΄ μ£Όμš” 둜직이 μ‹€ν–‰λ˜κΈ° 이전에 return을 ν•¨μœΌλ‘œμ¨ μ‹€ν–‰ costλ₯Ό 쀄이고 가독성을 λ†’μ΄λŠ” 방법이닀. def notify(data): if not data.get("to"): return False if data.get("platform") not in NOTIFY_PLATFORMS: return False return NOTIFY_PLATFORMS[data["platform"]](data) μœ„μ˜ κ²½μš°μ—μ„œ 보듯이 μ•Œλ¦Όμ„ μ£ΌλŠ” functionμ—μ„œ μ „μ²˜λ¦¬λ₯Ό 톡해 μ‹€μ œ 둜직이 μ‹€ν–‰ 되기 이전에 return을 μ£ΌλŠ” 것을 μ•Œ 수 μžˆλ‹€. μ•ˆμ’‹μ€ 예둜 보면 def notify(data): if not data.get("to"): return False elif data.get("platform") not in NOTIFY_PLATFORMS: return False else: return NOTIFY_PLATFORMS[data["platform"]](data) 와 같이 if-elif-else 둜 λͺ¨λ“  λ‘œμ§μ„ κ°μ‹ΈλŠ” 경우라고 생각 λ˜λŠ”λ°, 이 κ²½μš°λŠ” ν•΄λ‹Ή function의 정상 둜직이 μ–΄λŠ 뢄기인지 μ•ŒκΈ° νž˜λ“€λ‹€....

February 17, 2021 Β· 2 min Β· Dongwook Kim