HomeAboutSoftwarePublicationsPostsMicroBinfie Podcast

Lessons learnt about programming - exercise two

Posted on August 25, 2023
an AI generated picture (Midjourney) with prompt; 'computer programming in the style of Starry Night by Vincent van Gogh'. You can share and adapt this image following a CC BY-SA 4.0 licence.

This post is part of a series that summarised a workshop we ran recently. We were discussing programming, discussing common pitfalls, and then looked at some fun python tricks.

There are several sections:

What is wrong with this code?

Here is a simple error that you might encounter when programming. It's a simple error, but it's easy to make, and it's easy to miss. It's also easy to fix, once you know what to look for. What issues you can see?

N.B. We want to keep the original list as it is, make a copy of our original list, so that we can modify it with a new gene name.

python
# Creating a nested list
original_list = [["lacZ", "rpoB", "recA"], ["gyrA", "katG", "dnaA", "ftsZ", "ompF", "rpoD", "fabH"]]
new_copied_list = original_list
# Modifying the copied list
new_copied_list[0][0] = 'rpoH'
print("Original List:", original_list)
print("Copied List:", new_copied_list)

The output:

python
Original List: [['rpoH', 'rpoB', 'recA'], ['gyrA', 'katG', 'dnaA', 'ftsZ', 'ompF', 'rpoD', 'fabH']]
Copied List: [['rpoH', 'rpoB', 'recA'], ['gyrA', 'katG', 'dnaA', 'ftsZ', 'ompF', 'rpoD', 'fabH']]

But hang on, why does it work when I do something like this?

python
x = 'Hello'
y = x
y = 'Ciao!'
print(x, y)

The output:

python
Hello Ciao!

There are two things going on here at the same time. One is in the list example, the assignment of new_copied_list just means creating a new pointer to the same data in memory;

text
original_list ----> Location1
new_copied_list --------^

Any manipulation on one or the other changes the same data.

In Python, this can be confusing because the language does not expose pointers to the user. The alternative, which involves exposing pointers, would be even more confusing, so the language designers intentionally chose not to explicitly expose pointers. But, then why does it work for the String example? Because the integer is an immutable type. You can't make such a change to an integer as you do to a list, so python creates completed new copy for you. Is this confusing? Yes. Consider what happens when we change something that Python considers immutable, like a string:

python
x = 'Hello'
x[0] = 'G'

The output:

python
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-6758179335cb> in <cell line: 2>()
1 x = 'Hello'
----> 2 x[0] = 'G'
TypeError: 'str' object does not support item assignment

If this is still confusing, have a look at the concept of immutablity and mutability in programming.

Questions or comments? @ me on Mastodon @happykhan@mstdn.science or Twitter @happy_khan

The banner image is an AI generated picture (Midjourney) with prompt; 'computer programming in the style of Starry Night by Vincent van Gogh'. You can share and adapt this image following a CC BY-SA 4.0 licence.