目次
- 1 1. What Are Default Arguments in Python? [Beginner-Friendly Guide]
- 2 2. Basic Usage of Python Default Arguments
- 3 3. Common Mistakes and Precautions When Using Default Arguments
- 4 4. Evaluation Timing of Default Arguments and Its Effects
- 5 5. Practical Applications of Default Arguments in Python
- 6 6. Frequently Asked Questions (FAQ) About Python Default Arguments
1. What Are Default Arguments in Python? [Beginner-Friendly Guide]
When using Python functions, passing values to every argument can sometimes be inconvenient. This is where default arguments come in handy. By setting default arguments, you can omit parameters when calling a function, allowing you to write cleaner and more flexible code. In this section, we’ll explain the basic concept of default arguments and how to use them.1.1 What Are Default Arguments?
In Python functions, you can assign default values (initial values) to arguments. These are called “default arguments.” When you define a function with default arguments, you can omit certain parameters when calling the function, and the predefined default values will automatically apply.Basic Syntax of Default Arguments
def function_name(argument_name=default_value):
process
By using this syntax, you can make arguments optional, improving both readability and flexibility of your code.1.2 Basic Usage of Default Arguments
Let’s look at actual code examples to understand how default arguments work in Python.Basic Example
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!
greet("Sato") # Output: Hello, Sato!
In the greet()
function above, the default value of name
is set to "Guest"
. When the function is called without arguments, name
becomes "Guest"
. On the other hand, if you call greet("Sato")
, the specified value takes priority.Functions with Multiple Default Arguments
You can also set multiple default arguments.def introduce(name="Anonymous", age=20):
print(f"My name is {name}. I am {age} years old.")
introduce() # Output: My name is Anonymous. I am 20 years old.
introduce("Tanaka") # Output: My name is Tanaka. I am 20 years old.
introduce("Tanaka", 25) # Output: My name is Tanaka. I am 25 years old.
By assigning default values to multiple arguments, you can make your function calls more flexible.Benefits of Using Default Arguments
Using default arguments provides several benefits:- Simplifies code (optional arguments can be omitted)
- Prevents errors (avoids missing argument errors)
- Improves function versatility (default values allow for more flexible use)
1.3 Common Use Cases for Default Arguments
Default arguments are useful in many situations. Here are some common cases:1. Configurable Options
By preparing default values for options inside a function, you can easily allow users to override them.def download_file(url, timeout=10):
print(f"Downloading {url} within {timeout} seconds.")
download_file("https://example.com")
# Output: Downloading https://example.com within 10 seconds.
download_file("https://example.com", 5)
# Output: Downloading https://example.com within 5 seconds.
Here, the default value of timeout
is set to 10
. If no argument is provided, the timeout defaults to 10 seconds.2. Logging Messages
By assigning a default log level to functions, you can maintain cleaner and more consistent debugging output.def log_message(message, level="INFO"):
print(f"[{level}] {message}")
log_message("Process started")
# Output: [INFO] Process started
log_message("An error occurred", "ERROR")
# Output: [ERROR] An error occurred
In this example, level
defaults to "INFO"
, so unless specified otherwise, messages will be logged at the INFO level.1.4 Summary
Default arguments in Python are a convenient way to design flexible functions.- They allow functions to be called without specifying every argument
- Multiple default arguments can be set
- Simplifies code, improves readability and flexibility
- Useful for configurable options, logging, and many other cases
2. Basic Usage of Python Default Arguments
In the previous section, we explained the basic concept of default arguments in Python. In this section, we’ll look at how to use default arguments in practice with concrete code examples.2.1 Defining Functions with Default Arguments
Functions with default arguments can be defined as follows:def function_name(arg1=default_value1, arg2=default_value2, ...):
process
Example
def greet(name="Guest", message="Hello"):
print(f"{name}, {message}")
greet() # Output: Guest, Hello
greet("Sato") # Output: Sato, Hello
greet("Sato", "How are you?") # Output: Sato, How are you?
When arguments are omitted, the default values are applied. If arguments are provided, those values are used instead.2.2 Combining Default Arguments with Keyword Arguments
When calling functions, you can use keyword arguments for greater flexibility.Example Using Keyword Arguments
def introduce(name="Anonymous", age=20, country="Japan"):
print(f"My name is {name}. I am {age} years old, from {country}.")
introduce()
# Output: My name is Anonymous. I am 20 years old, from Japan.
introduce(name="Tanaka")
# Output: My name is Tanaka. I am 20 years old, from Japan.
introduce(age=25, name="Yamamoto")
# Output: My name is Yamamoto. I am 25 years old, from Japan.
introduce(country="USA", age=30)
# Output: My name is Anonymous. I am 30 years old, from USA.
➡ Keyword arguments let you call functions without worrying about argument order, improving readability.2.3 Practical Use Cases for Default Arguments
Default arguments can simplify code in specific scenarios.① Logging
By setting a default log level, you can standardize log management.def log_message(message, level="INFO"):
print(f"[{level}] {message}")
log_message("Starting application")
# Output: [INFO] Starting application
log_message("An error occurred", "ERROR")
# Output: [ERROR] An error occurred
② API Request Parameters
Setting a default timeout when calling APIs can help prevent errors.import requests
def fetch_data(url, timeout=10):
response = requests.get(url, timeout=timeout)
return response.json()
# Default 10-second timeout
data = fetch_data("https://api.example.com/data")
# Override timeout
data = fetch_data("https://api.example.com/data", timeout=5)
③ Simplifying Calculations
Default arguments can make calculation functions more flexible.def calculate_price(price, tax_rate=0.10):
return price * (1 + tax_rate)
print(calculate_price(1000)) # Output: 1100.0 (10% tax)
print(calculate_price(1000, 0.08)) # Output: 1080.0 (8% tax)
2.4 Summary
- Default arguments improve function usability
- Keyword arguments allow you to ignore argument order
- Useful in practical scenarios like logging, API requests, and calculations

3. Common Mistakes and Precautions When Using Default Arguments
Default arguments in Python are convenient, but incorrect usage can lead to unexpected bugs. In this section, we’ll explain important precautions, common errors, and how to avoid them.3.1 Pay Attention to the Order of Default Arguments!
In Python, arguments with default values must come after arguments without defaults. If the order is wrong, a SyntaxError will occur.❌ Incorrect Example (Causes Error)
def func(x=0, y): # Error: non-default argument follows default argument
print(x, y)
➡ Because x=0
comes before y
, Python raises a syntax error.✅ Correct Example
def func(y, x=0): # OK
print(x, y)
➡ Placing non-default arguments first avoids the error.3.2 Do Not Use Mutable Objects (Lists, Dictionaries) as Default Values
If you assign a mutable object (like a list or dictionary) as a default value, you may encounter unexpected behavior. This is because default arguments are evaluated once when the function is defined, and then reused each time.❌ Incorrect Example (Bug)
def add_item(item, item_list=[]):
item_list.append(item)
return item_list
print(add_item("apple")) # Output: ['apple']
print(add_item("banana")) # Output: ['apple', 'banana'] ← Unexpected behavior!
➡ The default list item_list=[]
is reused across calls, causing values to accumulate.✅ Correct Example
def add_item(item, item_list=None):
if item_list is None:
item_list = [] # Create a new list each time
item_list.append(item)
return item_list
print(add_item("apple")) # Output: ['apple']
print(add_item("banana")) # Output: ['banana'] ← Works as expected!
➡ By using None
as the default and creating a new list inside the function, you avoid this bug.3.3 Be Careful with Evaluation Timing of Default Arguments!
In Python, default argument values are evaluated when the function is defined, not each time the function is called. This can cause unintended behavior when using time-dependent values.❌ Incorrect Example (Bug)
import datetime
def log_message(message, timestamp=datetime.datetime.now()):
print(f"[{timestamp}] {message}")
log_message("First log")
log_message("Second log") # Same timestamp is reused!
➡ Since datetime.datetime.now()
is evaluated when the function is defined, all calls share the same timestamp.✅ Correct Example
import datetime
def log_message(message, timestamp=None):
if timestamp is None: # If None, assign current time
timestamp = datetime.datetime.now()
print(f"[{timestamp}] {message}")
log_message("First log")
log_message("Second log") # Different timestamps are used
➡ Using None
as the default and assigning inside the function ensures fresh values each time.3.4 Pay Attention When Combining with Keyword Arguments!
When using functions with default arguments, mixing positional and keyword arguments incorrectly can cause errors.❌ Incorrect Example (Error)
def greet(name="Guest", message="Hello"):
print(f"{name}, {message}")
greet("Hello", name="Sato")
# TypeError: multiple values for argument 'name'
➡ The string "Hello"
was passed as a positional argument for name
, then name="Sato"
was also given, causing a conflict.✅ Correct Example
greet(name="Sato", message="Hello") # OK
greet("Sato") # OK
➡ Carefully separate positional and keyword arguments to avoid duplication errors.3.5 Summary
- Default arguments must come after non-default arguments
- Do not use mutable objects (lists, dictionaries) as default values
- Default arguments are evaluated at definition time, not at call time
- Be careful when mixing positional and keyword arguments
4. Evaluation Timing of Default Arguments and Its Effects
In Python, default argument values are evaluated only once at the time of function definition, and the same object is reused every time the function is called. If you don’t understand this behavior, unexpected bugs can occur. In this section, we’ll explain evaluation timing in detail with examples, and show how to use it correctly.4.1 What Is Evaluation Timing of Default Arguments?
Normally, Python variables are evaluated fresh every time a function is called. However, default arguments are evaluated only once when the function is defined, which makes them different.Example of Evaluation Timing
import datetime
def get_current_time(timestamp=datetime.datetime.now()):
print(f"Current time: {timestamp}")
get_current_time() # Example Output: Current time: 2025-03-20 12:00:00
get_current_time() # Example Output: Current time: 2025-03-20 12:00:00 (same time!)
➡ Even when calling the function multiple times, the same timestamp is printed,
because datetime.datetime.now()
was evaluated once at function definition and reused.4.2 Problems Caused by Evaluation Timing
This behavior can lead to the following bugs.Problem 1: Mutable Objects (Lists, Dicts) Are Shared
def append_to_list(value, my_list=[]):
my_list.append(value)
return my_list
print(append_to_list(1)) # Output: [1]
print(append_to_list(2)) # Output: [1, 2] ← Unexpected!
print(append_to_list(3)) # Output: [1, 2, 3]
➡ A new list is not created each time. Instead, the same list is reused, causing data to accumulate.Problem 2: Dictionaries Show the Same Behavior
def add_to_dict(key, value, my_dict={}):
my_dict[key] = value
return my_dict
print(add_to_dict("a", 1)) # Output: {'a': 1}
print(add_to_dict("b", 2)) # Output: {'a': 1, 'b': 2} ← Unexpected!
➡ The dictionary is reused across function calls, instead of being re-created each time.4.3 How to Avoid Bugs Caused by Evaluation Timing
To prevent these problems, the best practice is to useNone
as the default value and create the object inside the function.✅ Correct Example (Solution)
def append_to_list(value, my_list=None):
if my_list is None:
my_list = [] # Create a new list each time
my_list.append(value)
return my_list
print(append_to_list(1)) # Output: [1]
print(append_to_list(2)) # Output: [2] ← Works correctly
print(append_to_list(3)) # Output: [3]
➡ By using None
, a new list is created every time, preventing shared-state bugs.4.4 Why Is None
the Best Choice?
In Python, None
is immutable and ensures that a new object is created whenever needed.
This approach has multiple benefits:- Prevents shared-state bugs
- Improves readability with explicit
if
checks - Allows safe dynamic creation of objects
Correct Example for Dictionaries
def add_to_dict(key, value, my_dict=None):
if my_dict is None:
my_dict = {} # Create a new dictionary
my_dict[key] = value
return my_dict
print(add_to_dict("a", 1)) # Output: {'a': 1}
print(add_to_dict("b", 2)) # Output: {'b': 2} ← Works correctly
➡ Each function call creates a new dictionary, so data is not shared between calls.4.5 Summary
- Default arguments are evaluated once at function definition and reused
- Mutable objects (lists, dictionaries) as defaults can cause shared-state bugs
- Best practice: use
None
and create new objects inside the function - This prevents unexpected bugs and makes your code safer
5. Practical Applications of Default Arguments in Python
So far, we’ve explained the basics and precautions of Python default arguments. In this section, we’ll look at how default arguments are used in real-world development. When used correctly, they can improve code readability and maintainability.5.1 Designing Functions with Default Arguments
Default arguments are often used to improve the usability of functions. They are especially useful in the following scenarios:① Functions with Configuration Options
In functions that include configurable options, default arguments allow for flexible design.def connect_to_database(host="localhost", port=3306, user="root", password=""):
print(f"Connecting to database: {host}:{port}, User: {user}")
connect_to_database()
# Output: Connecting to database: localhost:3306, User: root
connect_to_database(host="db.example.com", user="admin")
# Output: Connecting to database: db.example.com:3306, User: admin
➡ By providing default settings, functions can be easily used in standard cases.5.2 When to Use Default Arguments (and When to Avoid Them)
✅ Situations Where Default Arguments Are Useful
- When arguments are optional
- Example:
port=3306
,timeout=10
- When you want to provide frequently used defaults
- Example:
level="INFO"
for logging
- When you want to simplify function calls by defining standard behavior
- Example:
name="Guest"
❌ Situations Where You Should Avoid Default Arguments
- When the default value is a mutable object (list, dictionary, etc.)
- ➡ Use
None
and initialize properly inside the function
- When each function call should have a different value
- Example:
timestamp=datetime.datetime.now()
→ UseNone
to evaluate at call time
- When argument order is important
- ➡ Place default arguments at the end
5.3 Summary
- Using default arguments properly improves code readability and flexibility
- They are practical for database connections, API requests, logging, user input, and more
- Avoid pitfalls: don’t use mutable objects, and always pay attention to argument order
- Default arguments make functions more practical in real-world development
6. Frequently Asked Questions (FAQ) About Python Default Arguments
We’ve covered default arguments in detail, but in practice, several common questions arise. This section summarizes frequently asked questions about default arguments and their answers.6.1 Questions About the Basics of Default Arguments
Q1. Are there restrictions on the order of arguments with default values?
A: Yes. Arguments with default values must always come after arguments without defaults. If the order is wrong, Python raises aSyntaxError
. ❌ Incorrect Example (Error)def func(x=10, y): # Error: non-default argument follows default argument
print(x, y)
✅ Correct Exampledef func(y, x=10): # OK
print(x, y)
➡ The rule is that non-default arguments must come first.6.2 Questions About Mutable Objects (Lists, Dictionaries)
Q3. Why do bugs occur when using lists or dictionaries as default arguments?
A: Because Python evaluates default arguments once at function definition, and then reuses the same object. This means mutable objects like lists or dictionaries will be shared between calls. ❌ Bug Exampledef add_item(item, item_list=[]): # Dangerous
item_list.append(item)
return item_list
print(add_item("apple")) # Output: ['apple']
print(add_item("banana")) # Output: ['apple', 'banana'] ← Unexpected!
✅ Correct Example (Using None
)def add_item(item, item_list=None):
if item_list is None:
item_list = [] # Create a new list each time
item_list.append(item)
return item_list
print(add_item("apple")) # Output: ['apple']
print(add_item("banana")) # Output: ['banana'] ← Works correctly!
➡ Always use None
and initialize inside the function to avoid shared-state bugs.6.3 Other Questions About Default Arguments
Q5. When are default arguments evaluated?
A: Default arguments are evaluated once at the time of function definition. Therefore, if you expect different values for each call, you need to handle them carefully. ❌ Problematic Exampleimport datetime
def log_message(message, timestamp=datetime.datetime.now()):
print(f"[{timestamp}] {message}")
log_message("First log")
log_message("Second log") # Same timestamp reused!
✅ Correct Exampledef log_message(message, timestamp=None):
if timestamp is None:
timestamp = datetime.datetime.now() # Evaluate at call time
print(f"[{timestamp}] {message}")
log_message("First log")
log_message("Second log") # Different timestamps printed!
➡ Use None
as the default and evaluate inside the function when needed.6.4 Summary
- Default arguments must come after non-default arguments
- Keyword arguments let you ignore argument order when calling functions
- Never use mutable objects as defaults — use
None
instead - Default arguments are evaluated at function definition, not at call time
- You can even pass functions as default arguments for dynamic behavior
7. Conclusion and Next Steps
We’ve now explored Python’s default arguments in detail. By using default arguments appropriately, you can improve code readability, increase flexibility, and prevent bugs. Next steps:- Create functions that use default arguments and test their behavior
- Practice setting default values using
None
- Experiment with combining keyword arguments and default arguments in function design