A Nasty Feature of Python That Many Programmers Aren't Aware Of

...and it appears counterintuitive when they discover it.

Mutability is possibly one of the most misunderstood and overlooked concepts in Python.

Below, let’s discuss an example that many Python programmers struggle to understand.

Consider the following function:

Let’s invoke this a few times:

What happened there?

Why are multiple function invocations appending the passed arguments to the same list?

Can you figure it out?

If not, let’s understand it today.

In Python, the default parameters of a function are evaluated right at the time the function is defined.

This is in contrast to, say, C++, where the parameters are evaluated every time the function is called.

Thus, as soon as a function is defined, the function object stores the default parameters in its __defaults__ attribute.

We can verify this below:

In the above code:

  • We have defined a method my_function but haven’t invoked it.

  • Yet, the default parameters are accessible using the __defaults__ attribute.

This proves that the default parameters of a function are evaluated right at the time the function is defined.

As a result, if you specify a mutable default parameter in a function and mutate it, you unknowingly and unintentionally modify the parameter for all future calls to that function.

This is precisely what we saw earlier and has also been demonstrated below.

As depicted above, instead of creating a new list at each function call, Python continues to append the element to the same copy.

So what can we do to avoid this?

The solution is simple.

Avoid specifying any mutable default parameters in a function’s definition.

Instead, replace them with None.

Next, if the function does not receive a corresponding value during the function call, create the mutable object inside the function.

As shown above, we create a new list if the function didn’t receive any value when it was called.

This lets you avoid the unexpected behavior of mutating the same object:

👉 Over to you: What are some other nasty features of Python that many aren’t aware of?

👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights.

The button is located towards the bottom of this email.

Thanks for reading!

Latest full articles

If you’re not a full subscriber, here’s what you missed last month:

To receive all full articles and support the Daily Dose of Data Science, consider subscribing:

👉 Tell the world what makes this newsletter special for you by leaving a review here :)

👉 If you love reading this newsletter, feel free to share it with friends!

Reply

or to participate.