λ°μν
π python docstring references: https://wikidocs.net/16050
24. Docstring - λ¬Έμν
- νμ΄μ¬μ μ νλ©΄μ, μ¬λ¬κ°μ§λ‘ λ©μ§λ€λΌκ³ μκ°νλ κ² μ€μ νλ - docstringμ μμ±νλ©΄, νλ‘κ·Έλλ°μ μμ±μΌλ‘ μ κ·Όν μ μμ. ## 1. docstring μ΄λ …
wikidocs.net
- docstringμ λͺ¨λ, ν¨μ, ν΄λμ€ λλ λ©μλ μ μμ 첫 λ²μ§Έ λͺ λ Ήλ¬ΈμΌλ‘ λ°μνλ λ¬Έμμ΄ λ¦¬ν°λ΄
- Module 첫λ²μ§Έ μ€, ν¨μ μ μΈ ν λ΄λΆ λ°λ‘ μλ«μ€ λλ ν΄λμ€ μ μΈ ν λ΄λΆ λ°λ‘ μλ«μ€μ ν°λ°μ΄ν 3κ°λ μμ λ°μ΄ν 3κ°λ‘ μμ±νλ©΄ λλ€.
π Docstring μ¬μ© μμ
def hello():
"""μ΄ ν¨μλ 'Hello World!'λ₯Ό μΆλ ₯νλ€!"""
print('Hello World!')
hello()
# Hello World!
# μλ νμμ λν΄μλ pep8 Guide μ°Έκ³ : https://peps.python.org/pep-0008/
def hello(name: str) -> None: # Noneμ μ¨μ κ°κ°μ parameterκ° typeμ λ°λΌκ°λλ‘ code μ
λ ₯μ λ°λλ‘
if not isinstance(name, str):
raise TypeError("Name Must be a String!")
print(f"μλ
!,{name}")
hello("Lee")
# μλ
!,Lee
hello(123)
# TypeError: Name Must be a String!
π Docstring μ λ²μ©μ±
# λ²μ©μ±μ΄ λ¨μ΄μ§λ μμ
def add(a, b, c):
return a + b + c
print(add(1, 2, 3))
# 6
print(add(1, 2))
# TypeError: add() missing 1 required positional argument: 'c'
# 보μλ μ½λ - *args μ¬μ©
def add(*args):
print(args, type(args))
add(1, 2, 3, 4, 5)
# (1, 2, 3, 4, 5) <class 'tuple'>
def add(*args):
addition = 0
for number in args:
addition += number
return addition
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))
# kwargs : dictionary ννλ‘ λ°μ
# λ¨Έμ λ¬λ ν΄λμ€, νμ΄νΌνλΌλ―Έν° νλ μ, μ
λ ₯ κ°μ dictionaryλ£° κΈ°λ³Έκ°μΌλ‘ λ°κΈ° λλ¬Έμ
# => kwargs λ°λμ μ μ!
def temp(**kwargs):
for key, value in kwargs.items():
print(key, value)
temp(name="evan", age=30, city="Incheon")
λ°μν
'Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Python] Pandas data: auto-mpg data μκ°ν (0) | 2023.04.30 |
---|---|
[Python] matplotlib, seaborn λ§λκ·Έλν 그리기 / κΎΈλ―ΈκΈ° (0) | 2023.04.17 |
[Python] Pandas Data Analysis (0) | 2023.04.13 |
[Python] κΈ°μ΄λ¬Έλ² _ Sequence Type (List, Tuple, Dictionary) (3) | 2023.04.12 |
[Python] κΈ°μ΄λ¬Έλ² _ λ¬Έμμ΄ (String) (0) | 2023.04.12 |