Search HoldMyTwist

Browse all posts

Episode 34

The Basket with Two Names

Duck wants to eat two lunches, but there's only one basket. Can he tie a second ribbon to the basket's handle and get an extra lunch?

Episodes

Computing Basics

    The coral ribbon is attached to the handle of a basket that has two buns in it. The basket is being shown off proudly by duck from within the monitor. Robot's looking on from outside the monitor.
    Duck ties the aqua ribbon and ties it next to the coral ribbon on the same handle. They both point to the same basket with two buns.
    The coral tag, and aqua tag both point to the same handle. Duck is searching for another basket. But all he sees is the one basket. The two buns are in it. Duck seems to be a bit sheepish that he can't find the second basket.
    Duck is holding the pear but it's not in the basket yet. The basket has 2 buns still. Both ribbons are on the basket's handle.
    One pear goes in. The basket still has 2 buns and 1 pear. Both ribbons are still attached to the handle.
    The basket still has 2 buns and 1 pear. A coral ribbon and an aqua ribbon cross over the one handle of the basket. Duck is following the coral ribbon with his gaze, as he's looking at the basket from above.
    There's one basket beneath Duck's two ribbon ends. The basket still has 2 buns and 1 pear. Duck is sad.
    A coral ribbon and an aqua ribbon cross over the one handle of the basket. Duck is observing. There are two buns and one pear in one basket.
    Original basket still has 2 buns and 1 pear. It still has both coral and aqua ribbons. So far there's no second basket.
    The first basket still has two buns in it, and the one pear. Now the separate right basket also has 2 buns and 1 pear. The left handle has coral, aqua ribbons. The right handle has a mustard ribbon.
    Duck picks up the right one, using the handle with the mustard ribbon. The left one is left on the table. Both baskets have two buns and a pear inside.
    Duck is holding the purple plum in his hand over the right basket. The plum is not yet in the basket, it is waiting to be placed inside. Both baskets contain 2 buns and 1 pear. There are no changes on the left basket.
    The right basket contains four elements. A bun, another bun, a pear and a plum. Duck just put the plum in the right basket.
    High view, you can see there are two buns and a pear in the left basket, two buns, a pear and a plum in the right one.
    New example. We have 2 baskets. Each basket has 2 star tokens. In the left basket, we have 2 ribbons on one of the handles. For the right basket, we have a mustard ribbon on its own handle.
    Duck is getting ready to grab a star from the left basket. Both baskets still contain 2 stars. The right one has not been touched.
    1 star is in the left basket. 2 stars are in the right basket, still intact. Duck is holding the removed star outside the left basket. You can see all 4 original star tokens.
    Duck puts a different set of invitations next to the lunch baskets. The first basket still has two buns in it, and the one pear. The right basket contains four elements. A bun, another bun, a pear and a plum.

    Episode transcript

    Two ribbons, one handle

    The coral ribbon is attached to the handle of a basket that has two buns in it. The basket is being shown off proudly by duck from within the monitor. Robot's looking on from outside the monitor.

    Diagram values: P: [bun,bun]; a → P

    Duck: I need two lunches.

    Duck ties the aqua ribbon and ties it next to the coral ribbon on the same handle. They both point to the same basket with two buns.

    Diagram values: b = a; a → P; b → P

    Duck: Another name should do it.

    The coral tag, and aqua tag both point to the same handle. Duck is searching for another basket. But all he sees is the one basket. The two buns are in it. Duck seems to be a bit sheepish that he can't find the second basket.

    Diagram values: 1 BASKET; 2 NAMES

    Robot: Show me both of the baskets.

    Duck is holding the pear but it's not in the basket yet. The basket has 2 buns still. Both ribbons are on the basket's handle.

    Diagram values: b.append(pear)

    Duck: If I append pear by using b, what will a display?

    Follow the pear

    One pear goes in. The basket still has 2 buns and 1 pear. Both ribbons are still attached to the handle.

    Diagram values: P: [bun,bun,pear]

    Duck: One pear goes in.

    The basket still has 2 buns and 1 pear. A coral ribbon and an aqua ribbon cross over the one handle of the basket. Duck is following the coral ribbon with his gaze, as he's looking at the basket from above.

    Diagram values: a: [bun,bun,pear]; b: [bun,bun,pear]

    Robot: Both still link to the same basket.

    There's one basket beneath Duck's two ribbon ends. The basket still has 2 buns and 1 pear. Duck is sad.

    Duck: Two labels one handle.

    A coral ribbon and an aqua ribbon cross over the one handle of the basket. Duck is observing. There are two buns and one pear in one basket.

    Diagram values: b = a; a → P ← b

    Robot: Aliases. Not cloning the content of the basket by adding another name.

    Original basket still has 2 buns and 1 pear. It still has both coral and aqua ribbons. So far there's no second basket.

    Diagram values: P: [bun,bun,pear]

    Duck: Then I need another basket....

    A basket of its own

    The first basket still has two buns in it, and the one pear. Now the separate right basket also has 2 buns and 1 pear. The left handle has coral, aqua ribbons. The right handle has a mustard ribbon.

    Diagram values: c = a.copy(); P: [bun,bun,pear]; Q: [bun,bun,pear]

    Robot: copy it into a new list.

    Duck picks up the right one, using the handle with the mustard ribbon. The left one is left on the table. Both baskets have two buns and a pear inside.

    Diagram values: c → Q; a,b → P

    Duck: I can raise this basket.

    Duck is holding the purple plum in his hand over the right basket. The plum is not yet in the basket, it is waiting to be placed inside. Both baskets contain 2 buns and 1 pear. There are no changes on the left basket.

    Diagram values: c.append(plum)

    Robot: Add a plum to c. What will be left in a?

    The right basket contains four elements. A bun, another bun, a pear and a plum. Duck just put the plum in the right basket.

    Diagram values: P: [bun,bun,pear]; Q: [bun,bun,pear,plum]

    Duck: Put the plum into Q.

    High view, you can see there are two buns and a pear in the left basket, two buns, a pear and a plum in the right one.

    Diagram values: a,b → P: 3; c → Q: 4

    Robot: P has three entries. Q has four.

    The star test

    New example. We have 2 baskets. Each basket has 2 star tokens. In the left basket, we have 2 ribbons on one of the handles. For the right basket, we have a mustard ribbon on its own handle.

    Diagram values: x = [★,★]; y = x; z = x.copy()

    Duck: Going with 2 in each basket to start.

    Duck is getting ready to grab a star from the left basket. Both baskets still contain 2 stars. The right one has not been touched.

    Diagram values: y.pop()

    Robot: Remove one star using y, so how many stars will x and z have?

    1 star is in the left basket. 2 stars are in the right basket, still intact. Duck is holding the removed star outside the left basket. You can see all 4 original star tokens.

    Diagram values: x: [★]; y: [★]; z: [★,★]

    Duck: x has 1 star

    Duck puts a different set of invitations next to the lunch baskets. The first basket still has two buns in it, and the one pear. The right basket contains four elements. A bun, another bun, a pear and a plum.

    Robot: The second list of food items in memory doesn't create actual food.

    The lesson

    In Python if you say "b = a" you're just creating another name for the same list. So if you append to b you are changing the list a can see as well. If you do "a.copy()" you are making a new outer list. (In the story it's a flat list of simple things, so changing the length of the copy list does not affect the length of the original list. You can still have shared nested mutable things in a shallow copy. This story isn't about deep copy.) The baskets are about list identity in program memory as you're running, not about backup files or recovery. For example, no actual food is created when you make a copy of a list of foods in memory.

    Try it

    Create a list of three different coins, p. Set q to be the same list: q = p. Now create a copy of p: r = p.copy(). Finally add another coin to p. What's the size of q? What's the size of r? Why?

    Answer

    Now the list contains 4 elements. p and q point to the same list object (that is, this one). The list you got after copy, which is pointed by r, is a different object. This one contains 3 elements because the copied list has not changed.

    Sources

    Multiple names may refer to one mutable object; alias mutation is visible through both names. (opens in a new tab)

    Assignment binds rather than copies; list.copy creates a shallow copy. Nested objects may still be shared. (opens in a new tab)