FAQ: Learn Python: Function Arguments - Don't Use Mutable Default Arguments

This community-built FAQ covers the “Don’t Use Mutable Default Arguments” exercise from the lesson “Learn Python: Function Arguments”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Don’t Use Mutable Default Arguments

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

Hi, I was still a little confused on the concept so I searched further online and came upon this information I believe may be useful for future learners as they come to this point. I hope it helps, happy coding!

https://docs.python-guide.org/writing/gotchas/

Edit: not advertising, just info I found useful

6 Likes

The explanation says: ‘Sets and dictionaries are two other mutable objects in Python.’
what is meant with ‘Sets’?

A set is a “group” that contains non-repeated objects so lets think about the word

“banana” a set that contains its characters would be (“b”,“a”,“n”)

1 Like

Is there any example where this could be actually useful?

Very useful! This is the relevant text in your link, which really should have been mentioned in the Codecademy lesson:
“Python’s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.”

I think it would be worth pointing out in this exercise that

order2 = update_order({'item': 'soda', 'cost': '1.50'})

not only returns unexpected results for order2, but it will also update the value of order1.

I see that this is pointed out in the Hint, but I think that this is a really important point that should be belabored.

  1. Based on the hint, does that mean doesn’t matter what variable you call the list (order2, order1, or you can call it whatever), it will update for all variables? Did I understand it correctly?
  2. Why is that?
    Thanks in advance!