Closure là nested function "ghi nhớ" biến từ enclosing scope dù outer function đã kết thúc.
Lambda là anonymous function một dòng — thường dùng cho logic đơn giản tức thời.
python
# Closure — nhớ state
def make_counter(start=0):
count = [start]
def counter():
count[0] += 1
return count[0]
return counter
c = make_counter()
print(c(), c(), c()) # 1, 2, 3
# Lambda — function tức thời
double = lambda x: x * 2Lưu ý: Closure trong vòng lặp — mọi closure đều tham chiếu cùng biến vòng lặp, không phải giá trị tại thời điểm tạo.
Closure is a nested function that remembers variables from its enclosing scope.
Lambda is a one-line anonymous function.
python
def make_multiplier(n):
return lambda x: x * n # Closure over n
double = make_multiplier(2)
triple = make_multiplier(3)
print(double(5), triple(5)) # 10, 15Pitfall: Closures in loops capture the loop variable by reference, not by value.