Understanding assigment operator in Python from copying list or dictionary experience


When it comes to copy a variable into new variable, a few begginer will found that something weird happen when they modified the new variable.
Yes, the old variables state will get updated as well soon new variable edited. This is some basic example:

1
2
3
4
5
a = {1: "Hello"}
b = a
print(b[1])    # showing Hello
b[1] = "World"
print(a[1])    # showing World

Hey, what is this in Python? Fyi, the way we creating variable in Python is so simple like we create a “label” / “tag” or “pointer” which it’s simple reference to underlying object. So, as you see on example above, “a” and “b” is just simple reference that pointed into same object which have type “dictionary”. All changes that has been made in variable “b” will obviously affecting “a” which they pointed into same resources / object.

Apparently, assignment operator in Python have different function when dealing with immutable and mutable data type.
In the next example, i will use immutable data type assigment, eg:

1
2
3
4
5
a = b = 3
print(b)  # showing 3
b = 4
print(b)  # showing 4
print(a)  # showing 3

Hey, why it’s doesn’t have same effect as previous example? Yes, the way Python assigment operator treat the immutable and mutable data type is different. If we create and assign on immutable data type, then Python will create / copy with distinct value on each variable. Immutable means data can’t be changed, that’s why give distinct value on each variable is good practice.

But, when it comes on mutable which we can change the state, we back to C++ which we have variable and pointer. Pointer contain memory address of variable that pointed, not the “value” of variable. Confused? I hope not with this C++ example:

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;

int main() {
    int a = 3;
    int *pointerA;

    pointerA = &a;
    cout << pointerA;
    cout << *pointerA;
}

So, i create variable “a” with int as data type. Next line, I create a pointer variable “pointerA” with int as data type. I assign memory-address of “a” into “pointerA” using ampersand (&) which now “pointerA” contain value memory-address of “a”. This proved by first line of cout statement :

1
0x7fffabc80e44 ( memory address of "a")

To showing the value of “a” from pointer, i just dereferencing the pointer using asterix “*”:

1
3 (value of "a")

This approach is used in Python when we copying mutable variable into another variable. Basically, we just send “memory-address” of old variable object into the new variable.

Then the question how to “copy” distinct value of mutable type in Python properly? There are several ways which depend of the type of the mutable data type we used. For instance:

List data type:

1
2
3
4
5
a = [1, 2, 3]
b = list(a)
b[0] = 5
print(b)   / / [5, 2, 3]
print(a)  //  [1, 2, 3]

Dictionary data type:

1
2
3
4
5
a = {1: "Hello"}
b = a.copy()
b[1] = "World"
print(b[1])    # showing World
print(a[1])    # showing Hello

It always interesting when it comes to see how Python works in more depth level 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.