← Back to list

Stop Using Python Default Parameters Like This — Here’s Why

I’m pretty sure you’ve already used default parameters when writing your python functions. They’re convenient and powerful, but there’s a…

Flávio Farias · 2024-12-08 20:54 · 2 claps · 1.5 min read
#python-functions #default-arguments #mutable-objects #python-mistakes #object-reference
Open on Medium ↗

Stop Using Python Default Parameters Like This — Here’s Why

I’m pretty sure you’ve already used default parameters when writing your python functions. They’re convenient and powerful, but there’s a hidden pitfall that might be lurking in your code: default parameters with mutable values.

In The Hitchhiker’s Guide to Python![1], one of the most common mistakes highlighted is using mutable default arguments without understanding how they work. Let’s explore why this can lead to unexpected behavior.

Picture this: You’re asked to create a function that adds an element to a list and returns it. The list is optional. A straightforward approach might look like this:

def append_to(element, to=[]):
    to.append(element)
    return to

Task done, right? But have you considered what happens when you start using the function? Predict the output for the following:

list_1 = []

result_1 = append_to(1, list_1)
result_2 = append_to(10)
result_3 = append_to(22)

If you think the outputs will be:

[1]      # result_1
[10]     # result_2
[22]     # result_3

Sorry, you’re wrong! The actual outputs are:

[1]      # result_1
[10, 22] # result_2
[22, 22] # result_3

Why? The issue arises because Python evaluates default arguments once, when the function is defined — not every time the function is called. When you use a mutable default argument, like a list, any changes persist across future calls to the function.

This can lead to hard-to-debug bugs unless you’re intentionally reusing the same object. To avoid this problem, use an immutable default argument (e.g., None) and initialize the mutable object inside the function, as shown below:

def append_to(element, to=None):
    if to is None:
        to = []
    to.append(element)
    return to

With this approach, you’ll get the expected results:

[1]        # result_1  
[10]       # result_2  
[22]       # result_3 

By initializing the list inside the function, you ensure a new empty list is created each time the function is called. This small tweak makes your code safer, more predictable, and easier to maintain. So, the next time you use default parameters, handle them with care — especially if they’re mutable!

Happy coding! 🚀

References:

[1] The Hitchhiker’s Guide to Python!


메타데이터
post_id
3fb9d6eb6a6e
slug
stop-using-python-default-parameters-like-this-heres-why-3fb9d6eb6a6e
url
https://medium.com/@quirinoflavio/stop-using-python-default-parameters-like-this-heres-why-3fb9d6eb6a6e
canonical_url
https://medium.com/@quirinoflavio/stop-using-python-default-parameters-like-this-heres-why-3fb9d6eb6a6e
author_url
https://medium.com/@quirinoflavio
status
ok
fetched_at
2026-08-27 09:18:55