The tricky side of Python3: Mutable, Immutable!

Mutable vs Immutable

Introduction: What means “everything is an object”?

Immutable vs Mutable, how we can work with them while we are coding?

id and type

how to use id
How to use type

Immutable objects

List of Immutable and Mutable Objects
a = "banana"
b = "banana"
>>> a == b
True
>>> a is b
True
a & b refers to same thing: “banana” . this is happening when an object is immutable
# Assigning two objects to the same string
>>> a = "b"
>>> b = "b"
>>> id(a), id(b)
(139873424855424, 139873424855424)# Both strings have the same id
# Adding third object 'c'
>>> c = "c"
>>> id(c)
139873424786912
>>> a += c
>>> print(a)
bc
>>> id(a)
139873423703824

Mutable objects

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False
They reference to different object
>>> a = [1, 2]
>>> b = [1, 2]
>>> id(a)
140395896133320
>>> id(b)
140395896157448
>>> a += [3, 4]
>>> id (a)
140395896133320
>>> a = a + [5, 6]
>>> id (a)
140395896157640
We check that we are appending new items to the actual list, so the id() is the same
We can see that a refers to this new list with different id()

Extra point: We can also create a list and “referring” to another list. This is possible with the concept of “Aliasing”

>>> a = [1, 2, 3]
>>> b = a
>>> a is b
True
This MUTABLE lists has the same reference because we use the “Aliasing”

Why does it matter and how differently does Python treat mutable and immutable objects

mutable and immutable behavior
x = 'foo'
y = x
print x # foo
y += 'bar'
print x # foo

x = [1, 2, 3]
y = x
print x # [1, 2, 3]
y += [3, 2, 1]
print x # [1, 2, 3, 3, 2, 1]

def func(val):
val += 'bar'

x = 'foo'
print x # foo
func(x)
print x # foo

def func(val):
val += [3, 2, 1]


x = [1, 2, 3]
print x # [1, 2, 3]
func(x)
print x # [1, 2, 3, 3, 2, 1]

How arguments are passed to functions and what does that imply for mutable and immutable objects

>>> def increment(n):
... n += 1
>>> a = 3
>>> increment(a)
>>> print(a)
a = 3 # a is still referring to the same object
“a” and “n” refers to “3”
n refers to the new object, no more to “3”
a continuing referring to 3
>>> def increment(n):
... n += 1
... return n
>>> a = 3
>>> a = increment(a) # the return value of increment() is captured!
>>> print(a)
a = 4 # a now refers to the new object created by the function

Now let’s study what is happening when we pass a mutable object:

>>> def increment(n):
... n.append([4])
>>> L = [1, 2, 3] # mutable object (list)
>>> increment(L)
>>> print(L)
L = [1, 2, 3, 4] # we can see that a changed!
  • L refers to the list object that contains 3 immutable objects: integer 1, 2, 3. We can see also in a beauty graphic:
L is referring to a list object that contains 3 immutable object
after append() L and n is continuing referring to the same object

Finally, What means Small Integer Caching?

#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif

--

--

--

Software engineer in progress

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Problem Solving It

Git for beginners: flying in the clouds

It’s funny how much we as a society praise and value creativity, and yet seem to do as much as we…

Choose The Long Live Tech Stack

Watcher tryhackme walkthrough

Understand Python “yield”, An interrupt, A trap, A scissor

Docker Orientation and Setup

Happy Hacktoberfest 2020🥳

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Katherine Soto

Katherine Soto

Software engineer in progress

More from Medium

Python3: Mutable, Immutable… everything is object!

Git Commit Hash Number Theory

Why Learning Programming Takes Time

Un-Cringing An is_even() Function in Python