[2026] Python Functions | Parameters, Return Values, Lambdas, and Decorators
이 글의 핵심
Learn Python functions: *args/**kwargs, default arguments, lambdas, closures, decorators, recursion, and typing— with examples and pitfalls.
Introduction: Functions as the basic unit of code
Functions are reusable blocks of work. In Python, functions are first-class objects: you can assign them to variables and pass them as arguments.
1. Function basics
Defining and calling functions
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
def greet(name):
"""
Return a greeting for the given name.
Args:
name (str): Person to greet
Returns:
str: Greeting message
"""
return f"Hello, {name}!"
print(greet("Bob"))
Multiple return values (tuples)
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def get_user_info():
name = "Bob"
age = 25
city = "Seoul"
return name, age, city
name, age, city = get_user_info()
name, _, city = get_user_info()
Functions with no return value
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def print_hello():
print("Hello")
result = print_hello()
print(result)
print(type(result))
None patterns
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def find_user(user_id):
users = {1: "Bob", 2: "Alice"}
return users.get(user_id)
user = find_user(5)
if user is None:
print("Not found")
2. Parameters
Positional arguments
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Keyword arguments
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def introduce(name, age, city):
print(f"{name} ({age}) — {city}")
introduce("Bob", 25, "Seoul")
introduce(age=25, name="Bob", city="Seoul")
introduce("Bob", age=28, city="Daejeon")
Default arguments
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Bob"))
print(greet("Bob", "Hi"))
Never use a mutable default: 아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def add_item_bad(item, items=[]):
items.append(item)
return items
def add_item_safe(item, items=None):
if items is None:
items = []
items.append(item)
return items
*args and **kwargs
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
def sum_all(*numbers):
return sum(numbers)
def print_info(**kwargs):
for k, v in kwargs.items():
print(f"{k}: {v}")
def complex_func(a, b, *args, **kwargs):
print(a, b, args, kwargs)
complex_func(1, 2, 3, 4, x=5, y=6)
Parameter order (typical): positional → *args → defaults (careful with Python 3.8+ keyword-only) → **kwargs.
3. Lambda functions
아래 코드는 python를 사용한 구현 예제입니다. 반복문으로 데이터를 처리합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
square = lambda x: x ** 2
print(square(5))
students = [("Bob", 85), ("Alice", 90), ("Carol", 80)]
print(sorted(students, key=lambda x: x[1], reverse=True))
numbers = [1, 2, 3, 4, 5]
print(list(map(lambda x: x ** 2, numbers)))
print(list(filter(lambda x: x % 2 == 0, numbers)))
Prefer list comprehensions over map/filter when readability wins.
4. Closures
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def make_multiplier(n):
def multiply(x):
return x * n
return multiply
times_3 = make_multiplier(3)
print(times_3(10))
nonlocal
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def make_counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
5. Decorators (introduction)
Decorators wrap functions to add behavior (logging, timing, auth) without editing the original function’s body. 아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
def my_decorator(func):
def wrapper(*args, **kwargs):
print("before")
result = func(*args, **kwargs)
print("after")
return result
return wrapper
@my_decorator
def add(a, b):
return a + b
Examples: timing and caching
다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 함수를 통해 로직을 구현합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
import time
from functools import lru_cache
def measure_time(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"{func.__name__}: {time.time() - start:.4f}s")
return result
return wrapper
@lru_cache(maxsize=128)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
6. Recursion and higher-order functions
아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
def apply_operation(func, value):
return func(value)
Best practices
- Clear names, single responsibility, docstrings.
- Avoid mutable defaults; use
Noneand create a new list/dict inside. - Type hints (
typing) help IDEs andmypy. - Prefer pure functions when possible.
Summary
- Define with
def, return withreturn(implicitNoneif omitted). - Positional, keyword, defaults,
*args,**kwargs. - Lambdas for tiny expressions; comprehensions often clearer than
map/filter. - Closures +
nonlocalfor enclosed state. - Decorators compose behavior; use
functools.wrapsin production (see advanced post).