5
Lesson 5
Default parameter values
In Python, default parameter values allow you to specify a default value for one or more parameters in a function definition. This means that if a caller doesn’t provide a value for a parameter with a default, the function will use the default value instead. Default parameters make functions more flexible and reduce the need for the caller to specify every argument.
1. How to use default parameter values:
To set a default parameter value, assign a value to a parameter in the function definition. Here’s the syntax:
1. How to use default parameter values:
To set a default parameter value, assign a value to a parameter in the function definition. Here’s the syntax:
def function_name(parameter=default_value): # Function body
If you don’t pass a value for parameter when calling the function, it will use default_value. If you do pass a value, it overrides the default.
Example: Simple greeting function
Let’s create a function that greets a user. It has a default parameter value for greeting.
def greet_user(name, greeting="Hello"): print(f"{greeting}, {name}!")
In this example:
- greeting has a default value of "Hello".
- name has no default, so it’s required when calling the function.
2. Calling the Function:
a. Without specifying greeting:
greet_user("Alice") # Output: Hello, Alice!
Here, greeting uses its default value "Hello", so the output is "Hello, Alice!".
b. By specifying greeting:
greet_user("Bob", greeting="Hi") # Output: Hi, Bob!
Here, we pass "Hi" for greeting, overriding the default value.
3. Multiple default parameters:
You can also define multiple default parameters. Let’s modify the function to allow a customizable punctuation mark.
def greet_user(name, greeting="Hello", punctuation="!"): print(f"{greeting}, {name}{punctuation}")
Now, greeting defaults to "Hello" and punctuation defaults to "!".
Calling the Function:
a. Using all defaults:
greet_user("Alice") # Output: Hello, Alice!
b. Overriding some defaults:
greet_user("Bob", greeting="Hi") # Output: Hi, Bob! greet_user("Charlie", punctuation=".") # Output: Hello, Charlie. greet_user("Diana", greeting="Hey", punctuation="?") # Output: Hey, Diana?
4. Important rules with default parameters:
Default Parameters Must Come After Non-defaults: In a function definition, parameters with default values must appear after parameters without default values. This is to avoid ambiguity in how arguments are assigned.
def greet_user(greeting="Hello", name): # ❌ SyntaxError # Function body
The correct order is:
def greet_user(name, greeting="Hello"): # ✔️ # Function body
Mutable default values caution: Be cautious when using mutable objects (like lists or dictionaries) as default parameter values because they retain their state across multiple function calls.
def add_to_list(item, my_list=[]): my_list.append(item) return my_list print(add_to_list("apple")) # Output: ['apple'] print(add_to_list("banana")) # Output: ['apple', 'banana']
In this case, my_list retains its changes between calls. To avoid this, use None as the default and then initialize inside the function:
def add_to_list(item, my_list=None): if my_list is None: my_list = [] my_list.append(item) return my_list
Summary
- Default parameter values let you set a default for a parameter, making it optional for the caller.
- Order matters: Parameters with defaults must come after required parameters.
- Care with mutable defaults: Using mutable objects as default values can lead to unexpected behavior.
Default parameters make functions versatile, allowing you to keep the code simple and to add flexibility by providing options only when needed.