Copying and flipping

Copying and flipping#

We saw earlier that modifying the 2D list image in the function modified rgb_square. We can solve this by making a copy of the image and returning the copy.

Below is the modified version of flip_left_right. Notice that we have changed the function to first make a copy of img. Instead of modifying img, we modified copy_img and returned copy_img which will prevent our original img from being modified.

Our updated code prints the following. Notice that the argument, rgb_square, remained the same after we called the function and the new variable, rgb_square_flipped, stores the return of flip_left_right(rgb_square).

rgb_square before flip_left_right = [[(255, 0, 0), (0, 255, 0), (0, 0, 255)],
                                    [(255, 0, 0), (0, 255, 0), (0, 0, 255)],
                                    [(255, 0, 0), (0, 255, 0), (0, 0, 255)]]
rgb_square after flip_left_right = [[(255, 0, 0), (0, 255, 0), (0, 0, 255)],
                                    [(255, 0, 0), (0, 255, 0), (0, 0, 255)],
                                    [(255, 0, 0), (0, 255, 0), (0, 0, 255)]]
rbg_square_flipped = [[(0, 0, 255), (0, 255, 0), (255, 0, 0)],
                    [(0, 0, 255), (0, 255, 0), (255, 0, 0)],
                    [(0, 0, 255), (0, 255, 0), (255, 0, 0)]]

Why did this work? In the previous lesson, we learned that img and rgb_square are actually references to the same object. By creating a copy of img, we have created a new 2d list object that contains the same elements as img. Now, copy_img references this new object. When we return copy_img, rgb_square_flipped will contain the same reference as copy_img; hence, rbg_square_flipped will reference the new object that was created in the function.

Keep in mind that copy_img and img are local variables to flip_left_right and only exist within the function. We cannot access copy_img and img outside of flip_left_right.

Optimization: copy on-the-fly#

Our code above copies and then flips. We could work differently, copying on the fly:

Notice how we avoid a lot of work: we don’t have to initialize copy_img up front; we don’t have to worry about temporary variables or stopping in the middle. It’s often much easier to write copying code than to do things in place!