Default mutable parameter in python

This is one issue that you might come across one time by accident and then never again. I mentored a student that had an implementation like this:

def f(foo, bar = []):
    pass

I pointed out the issue and he fixed it. He didn’t actually need to have that second parameter at all, so I guess it’s not a common occurrence in this exercise.

So, I would like to think of a problem that actually needs a function like this to exist and forces the students to make this mistake so that they learn how to properly approach a default mutable parameter.

I’m open to suggestions, as I haven’t been able to think of one answer.

The Python track has a very long list of concepts and things they’d love to add. Mutability would be nice to add, but probably wouldn’t rank high on the list.

As a Python track maintainer, I can say that mutable default arguments (and mutability in general) have been brought up many times before - and we wouldn’t consider a mutability exercise before we completed what we have logged in the repo (and what we have on our internal improvement list).

We have a huge amount of “core” exercises and concepts we really need to get to first, to fill out basic data structures and concepts. While almost everyone gets caught with the mutable default, it is usually just once.

Its a great thing to demonstrate, but I think having student do exercises on things like error handling and dictionary methods need to come first.