For more information on concepts covered in this lesson, you can check out Immutability in Python.
Recognizing Unsuitable Default Data Types
00:13 These are not the only data types you can use as default values. However, not all types should be used. In this section, you’ll see why mutable data types should not be used as default values in function definitions.
You can now pass
shopping_list to the function when you call it. This makes the function more self-contained, as it doesn’t rely on a variable called
shopping_list to exist in the scope that’s calling the function. This change also makes the function more flexible, as you can use it with different input dictionaries.
You’ve also added the
return statement to return this modified dictionary. This line is technically not required at this stage, as dictionaries are a mutable data type, and therefore the function will change the state of the dictionary that exists in the main module. However, you’ll need the
return statement later when you make this argument optional, so it’s best to include it now. To call the function, you’ll need to assign the data returned by the function to a variable.
You can also add a
shopping_list parameter to
show_list(), the first function you defined in this course. You can now have several shopping lists in your program and use the same functions to add items and display these shopping lists.
The list of items to buy from the hardware store is shown first. The second part of the output shows the items needed from the supermarket. You’ll now add a default value for the parameter
add_item() so that if no dictionary is passed to the function, then an empty dictionary is used.
03:46 When you run this script, you’ll get the output seen on-screen, showing the items needed from the clothes shop, which may give the impression that this code works as intended. However, this code has a serious flaw that can lead to unexpected and incorrect results.
This problem happens because dictionaries are a mutable data type. You assigned an empty dictionary as the default value for the parameter
shopping_list when you defined the function. The first time you call the function, this dictionary is empty. However, as dictionaries are a mutable type, when you assign values to the dictionary, the default dictionary is no longer empty. When you call the function the second time, and the default value for
shopping_list is required again, the default dictionary is no longer empty, as it was populated the first time you called the function. Since you’re calling the same function, you’re using the same default dictionary that’s stored in memory.
This behavior doesn’t happen with immutable data types. The solution to this problem is to use another default value, such as
None, and then create an empty dictionary within the function when no optional argument is passed.
You can check whether a dictionary has been passed as an argument using the
if statement. You shouldn’t rely on the falsy nature of
None, but instead explicitly check that the argument is
None. Relying on the fact that
None will be treated as a false value can cause problems if another argument that is falsy is passed. Now, when you run your script again, you’ll get the correct output since a new dictionary is created each time you use the function with the default value for
06:14 The takeaway from this is that you should always avoid using a mutable data type as a default value when defining a function with optional parameters. In the next section of the course, you’ll take a look at error messages related to input arguments.
Become a Member to join the conversation.