Python › Functions & Scope

How do default parameter values work, and when are they evaluated?

Beginner

Answer

A default value (`def func(param=default):`) is used when the caller omits that argument — critically, the default expression is evaluated ONCE, at FUNCTION DEFINITION time, not on every call, which matters for mutable defaults.
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Sam"))              # 'Hello, Sam!' -- uses the default
print(greet("Sam", "Hi"))        # 'Hi, Sam!' -- default overridden
Real-world example Providing a sensible default value for an optional configuration parameter, like a default timeout or retry count.

Common follow-ups: Why is using a mutable object (like a list) as a default value considered a classic Python pitfall?