โ๏ธ Python Default Arguments โ Complete Guide with Examples
๐งฒ Introduction โ Why Use Default Arguments?
In Python, functions are powerful because they offer flexibilityโand default arguments make them even more adaptable.
With default arguments, you can assign a default value to parameters. This lets users call your function with fewer arguments while still giving them the option to override those defaults.
Default arguments are essential when designing APIs, utilities, or any code that benefits from optional parameters.
๐ฏ In this guide, you’ll learn:
- How default arguments work in Python
- The order and rules of default parameters
- Real-world use cases
- Best practices and common pitfalls
๐ง Syntax of Default Arguments
def function_name(param1=value1, param2=value2):
# function body
- Parameters with default values must come after non-default parameters.
โ Example 1: Function with One Default Parameter
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!
greet("Alice") # Output: Hello, Alice!
๐ Explanation:
If no argument is passed, "Guest"
is used by default.
๐งฎ Example 2: Multiple Default Parameters
def power(base, exponent=2):
return base ** exponent
print(power(3)) # 9
print(power(3, 3)) # 27
๐ก Use Case:
Let users compute square by default, or choose their own exponent.
โ Example 3: Incorrect Parameter Order
# โ This will raise a SyntaxError
# def example(a=1, b):
# pass
โ ๏ธ Warning:
Default arguments must come after all required (non-default) ones.
๐ Real-World Example: Sending Emails with Optional Subject
def send_email(to, subject="No Subject", body=""):
print(f"To: {to}")
print(f"Subject: {subject}")
print(f"Body: {body}")
send_email("user@example.com")
Output:
To: user@example.com
Subject: No Subject
Body:
โ
Tip: Default arguments are ideal for optional parameters like subject
, headers
, or timeout
.
๐ Combining Positional and Default Arguments
def book_flight(source, destination="New York"):
print(f"Booking flight from {source} to {destination}")
book_flight("London") # destination defaults to New York
book_flight("London", "Chicago") # override default
๐ Summary โ Key Takeaways
- โ Use default arguments to make parameters optional
- ๐ Default values are assigned only if no argument is passed
- โ ๏ธ Always put default parameters after non-default ones
- ๐ฆ Great for setting fallback values and simplifying function calls
โ FAQ Section
โ Can default arguments be functions or objects?
Yes, but mutable defaults (like lists or dicts) should be avoided directly. Use None
and set defaults inside the function.
โ Can I override default arguments?
Yes! Just pass a value in the function call.
โ Are default arguments stored in memory?
Yes, the default values are evaluated once at function definition time.
โ Can I mix default and non-default arguments?
Yes, but all non-default arguments must come first.
โ How are default arguments different from *args
and **kwargs
?
default
arguments give fallback values.*args
accepts variable non-keyword arguments.**kwargs
accepts variable keyword arguments.
Share Now :