Mutable Collection Types
00:00 Previously, you saw how passing the mutable objects of custom classes to functions in Python mimics pass by reference. In this lesson, you’ll look at how passing mutable collection type objects does so as well. Recall that sets, lists, and dictionaries are mutable objects in Python.
00:19 This means you can modify them in place as opposed to creating new objects and rebinding variable names. So you can create a list, use an index to look at specific elements, and even change individual elements.
00:45 And with a set, you can make a set and call methods on it to modify it.
01:04 In these instances, new objects are not being created. Python is modifying the object each variable is bound to. This won’t work with tuples and strings. You can create a tuple,
01:21 access individual elements using an index, but you can’t change an element using an index. Same thing for strings. You can create a string, access individual characters using an index, but you cannot modify individual characters.
01:49 So, writing a function to modify in place an object passed as an argument will work with sets, lists, and dictionaries, but not with tuples and strings.
So, for example, you can write a function which takes a dictionary as an argument and modify it in place, which is just like using pass by reference. Here’s a function that takes a dictionary as a parameter, then squares the value associated with the key
02:17 While it might not be practical to be squaring elements of a dictionary, the point is we’re able to pass this dictionary as an argument and modify it like pass by reference would.
We call the dictionary passed with the parameter name
num_dict, find the value associated with the key
"n", multiply it by itself, then map the key
"n" back to that squared value.
And here’s a script similar to all the ones you’ve seen before. We create a dictionary which maps
4, pass that dictionary as an argument to the
square() function, which will find the
4 and square it, and then we will print the result to see that that value has actually been squared.
03:07 And since I’m already in my REPL, I’ll let ptpython run the script when I import it.
And we see the dictionary with the value mapped to
16, although it might look cleaner to just use the value, printing just the value associated with the key
Alternatively, you could write a function that squares in place the first element of a list. Pass a list as a parameter, access the first entry, the one with index
0, multiply it by itself and save it back to that same position.
Here, we create a list, pass it to the
square() function, which will square the first element, and then print it to see that it works.
When I import it, the script runs and we see the list with the one and only element
16. And if I just want the element itself, I can refer to it by its index.
04:25 So now you’ve seen, when using mutable objects as arguments, pass by assignment mimics pass by reference. And you’re done! Next, we’ll review what you’ve learned and wrap things up.
Become a Member to join the conversation.