9.11. Mutable Object Side Effects

When we pass mutable objects, like lists, to functions, the contents of those objects can be changed within the function. This is a side effect, if the object is not returned from the function. Consider the following example, which we have seen before:

Activity: CodeLens 9.11.1 (clens9_10_1)

When this code runs and we print out mylst, it looks different from what’s in mylst on line 5. Again, in this example, the bison function is right there, so it isn’t hard to figure out why the list changed, but if there were 100 lines of code between the function definition and the function being called, it would be harder to understand and debug.

We can avoid confusing side effects when passing around mutable objects by passing in a copy of the object and then returning the modified copy and reassigning it to the variable. This makes it much more obvious that the function is altering the mutable object. Here is the code above rewritten to do this:

Activity: CodeLens 9.11.2 (clens9_10_2)

On line 7 in the code above, we use the built-in list() function, which takes a sequence as a parameter and returns a copy of that sequence as a list. Note also that we had to modify the function to return the list. Once these changes are made, the alterations to the list are no longer considered a side effect, as they are made transparent by returning a changed value.

9.11.1. Side Effects in Interactive Programs

When we program interactively and use listener functions, we end up modifying global variables and objects and creating side effects. This is because the listener functions that you can register must only have the parameters relevant to the interaction event. Let’s revisit an earlier example:

In this interactive programming example, it isn’t possible to pass the turtle object (alex) to the listener functions key_r() or click() because these functions’ parameters are confined to the parameters needed for responding to specific events. The key_r function is registered to execute when the user presses the r key and as a key listener function it takes no parameters. The click function is registered to execute when the user clicks the mouse and as a mouse-click listener function it must take two parameters so the operating system can pass in the information about where the user clicked the mouse (x and y). We can make this code have fewer side-effects by modifying it as shown below.

So, in the code above, we specify that we want to access and edit the global turtle variable (alex) in the listener functions key_r() and click(). Inside those functions, we call the other functions, but now we are passing them the turtle, they are modifying the turtle, and returning the turtle back to us. Now the only side-effects are the effects on the canvas (the square that is drawn, and the color and position of the turtle triangle representation). By returning the turtle object to the listener, we make it really clear that the turtle is changed by the random_square(), random_location() and random_colour() functions.

Check your understanding

You have attempted of activities on this page