aranthe ([personal profile] aranthe) wrote in [community profile] intro_to_cs 2010-01-18 07:49 pm (UTC)

I get the visualization thing. I do it myself. :-) About the shark and remora analogy, just remember that the remora isn't attached to the shark at all. Here's another way to visualize the concept which works for any type of change, not just for appending:

Suppose you create a bouquet which contains a white carnation, a red rose, a yellow tulip and an orange lily. (Not very likely, but...) You tie a tag around the bouquet and label it bouquet (the tuple). Also, each flower has its own tag: bouquet[0], bouquet[1], etc. These are the elements of the tuple.

When you unpack the tuple, you add a second tag to each of those flowers. Maybe you name them by variety carnation, rose, etc.

Now, suppose you'd prefer a purple carnation and decide to use the old food coloring trick to change the color of the carnation. Whoops! Python says that you can't change the tuple (bouquet) or any of its parts. Instead, you have to get another white carnation and dye it. When you do that, Python moves the carnation tag from the white carnation in the tuple bouquet and gives it to the purple carnation. The white carnation is still bouquet[0], but the carnation tag is now around the purple carnation.

In other words, the tuple and its elements are "frozen." Using the unpacked names, you can make changes, but when you do, the changed data is completely independent of the original.


Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting