기본 문법
pass
/ 앞: 위치 전용 – 순서대로만
/와 * 사이: 자유 – 둘 다 가능
* 뒤: 키워드 전용 – 이름=값으로만
키워드 전용 (*)
pass
connect(host=”localhost”, port=8080) # ✓
connect(“localhost”, 8080) # ✗
위치 전용 (/)
return x ** y
pow(2, 3) # ✓
pow(x=2, y=3) # ✗
혼합
pass
process(1, 2, c=3) # ✓
process(1, b=2, c=3) # ✓
process(a=1, b=2, c=3) # ✗ a는 위치 전용
process(1, 2, 3) # ✗ c는 키워드 전용
패턴 정리
| 목적 |
문법 |
| 모두 자유 |
def f(a, b, c) |
| c만 키워드 전용 |
def f(a, b, *, c) |
| a만 위치 전용 |
def f(a, /, b, c) |
| 혼합 |
def f(a, /, b, *, c) |
| 모두 키워드 전용 |
def f(*, a, b, c) |
참고
- PEP 3102 – Keyword-Only Arguments (Python 3.0)
- PEP 570 – Positional-Only Parameters (Python 3.8)
답글 남기기