Today on “Bugs that everyone already knew about but Maia found out about for the first time so will write up anyway”, we’re going to talk about the perils and pitfalls of using mutable objects as default arguments for functions in Python. (This episode brought to you by Maia’s contradance database and the letter Y.)
No, nothing went wrong in my code that caused me to learn this lesson, but multiple code reviewers raised red flags about this line in my code:
def resolve_query_dict(d, moves_list=Move.objects.all()).
Move.objects.all(), by the way, is a fancy Django function that returns a list of all of the
Move objects in your database, and since I would likely never be running this code over an extended period of time while things were added to the database, accidentally freezing the value of “all of the moves in my database” wouldn’t have really been an issue, but the dangers of writing code like the above are still whacky and interesting. Basically, odd things happen when you use mutable objects or called functions as default variables in your functions.