I is for immutable - Python A to Z
Code in this blog post was written with versions: Python: 3.14
Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month.
When we start learning programming, one of the first things we’re taught is a concept of variable. Variables are very handy: they store some data in memory, have a name you can use to refer to them later in the code and they can change.
That’s what variable means. Merriam-Webster defines the word as
able or apt to vary : subject to variation or changes
A classic example is a counter that goes up.
counter = 0
print(counter) # 0
counter = counter + 1
print(counter) # 1
counter = counter + 1
print(counter) # 2
counter = counter + 1
print(counter) # 3
Most of the early days, months or years of learning programming is about understanding this idea and a few related bits like different types (numbers, strings, lists, dictionaries, booleans etc), conditionals and loops. After you learn those, the rest of your career is just banging your head to the wall when the simple constructs you wrote don’t work (or you can talk to a duck and save a headache).
You build your internal schema of how to program based on the idea of a variable.
Functional programming
Then at some point — maybe at an advanced class at the university, at a meetup or in the always peaceful and polite discussion forums on the web — you’re introduced to the idea of immutability (not capable of or susceptible to change).
Someone comes hype the idea of functional programming that you’ve never heard of. They start talking about how changing variable state is so error-prone and pure functions is where the game should be played at.
A pure function is a function that given an input, always returns the same output. There are no side effects. It only relies on the inputs provided (and doesn’t read anything from disk, other parts of memory, make any web calls and so on) and is deterministic in crafting its output.
Pure functions are way easier to test, have less unexpected issues and can make your code easier to reason with. The hardcore functional programming people don’t even store values in temporary variables. They call functions, pass the results to other functions and beauty emerges and birds sing.
But if your entire schema of how programming works was laid on the foundations of variables, it can be quite a head scratcher to start adopting. I was on my third year at the university studying computer science when I took a functional programming course and oh boy it was a doozy.
I don’t know if that course was a blessing or a curse. There were maybe easier ways to get into understanding immutability and pure functions in programming but on the other hand, diving directly to the deep end and learning to swim might have been a good form of shock therapy.
I’m not a purist but I do prefer as much functional style as possible — mostly to help make code easier to test and thus reducing the stress of growing complexity.
Tuples are great, m’kay
Some data structures are immutable: once you make it, you gotta stick with it.
In Python, we have tuples (and their more eloquent heir namedtuples). You cannot change the contents of a tuple, no matter how hard you try.
date = (2026, 8, 10) # parentheses are optional but preferable
# Access them by index
print(date[0]) # 2026
# Can't change though
date[0] = 2025
# TypeError: 'tuple' object does not support item assignment
I really like tuples. Especially namedtuples.
They also make me appreciate Python’s beautiful design. All you really need is a comma in between two expressions and you’ve got yourself an immutable tuple.
If you want to return multiple values from a function, it’s tuple time!
from typing import Tuple
def squares_and_cubes(number: int) -> Tuple[int, ...]:
return number, number**2, number**3
Back in the day, I started with PHP and a bit later with Java at high school. When I moved into Python and learned about how easy it is to return multiple values through a tuple without having to explicitly craft a tuple, I started appreciating the language even more.
If something above resonated with you, let's start a discussion about it! Email me at juhis@hamatti.org and share your thoughts. This year, I want to have more deeper discussions with people from around the world and I'd love if you'd be part of that.