Opening Scenario

You're caching configuration in a dict keyed by environment:

# Python
default_config = {"timeout": 30, "retries": 3}
configs = {
    "dev":  default_config,
    "prod": default_config,
}

# Later, someone edits prod
configs["prod"]["timeout"] = 60

print(configs["dev"])   # {'timeout': 60, 'retries': 3} — dev changed too?!

You meant to give each environment its own config. Instead, both keys point to the same dict, and a write through "prod" bled into "dev". In the review, someone says "you need to copy the dict." Someone else says "actually, you need an immutable config object." A third voice says "Java would have the same bug — but Kotlin's data class would save you."

Why is "just don't mutate shared state" so much harder than it sounds?


Question Progression

Q1: What does "mutable" actually mean at the memory level?

Question: We say a list is mutable and a string is not. The list lives on the heap; so does the string. Why can we change one and not the other?

In the full tutorial

  • When to Reach for This

Glossary history

Click dotted jargon to save explanations here.