Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Experiment - Names, objects, and execution order

Names, objects, and execution order

You now know that objects can change. This notebook lets you inspect one change at a time instead of running a whole file at once. First read the saved outputs; they are available even without an interactive kernel.

Use the page’s execution control if browser execution is available. For work you want to keep, use the notebook download control, save under student-work/foundations, open it in VS Code, and select your .venv Python kernel at the top right. Each code cell has a run button. Restart Kernel and Run All resets the experiment. Browser edits are temporary; save a local copy before closing.

Predict a change

Will alias have three items after we append through readings? Will separate? Write a prediction before running the following cell.

Original: [False, True, False]
Alias: [False, True, False]
Separate copy: [False, True]

alias refers to the same list, so it sees the appended item. copy() makes a new list. Here that is enough because the items are booleans; this is a shallow copy, so nested mutable objects would need more care.

Notice cell state

Run the next cell twice. Predict both lengths. The second run uses the state left by the first run, even if you didn’t change any code.

Length now: 4

From a fresh top-to-bottom run, the length is 4. Running only the last cell again makes it 5. Rerun the first code cell to recreate the lists, or restart the kernel and run all. This is why a result from an old notebook state can be misleading.

Now put the same statements in a normal .py file and run that file twice. Each fresh Python process starts the program again; it won’t keep the previous list. Write down why the two names share changes, then explain how you would create an independent list.