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

Introduction

Identity

An object’s identity never changes once it has been created. You may think of it as the object’s address in memory. The is and is not operator compares the identity of two objects. The id() function returns an integer representing its identity.

>>> a = 1
>>> id(a)
10105088

Type

An object’s type defines the possible values and operations (e.g. “does it have a length?”) that type supports. The type() function returns the type of an object. An object type is unchangeable like the identity.

>>> a = 1
>>> type(a)
<class 'int'>

Mutable and immutable objects

Every variable holds an object instance that’ll later on and during initiation will be assigned. so what’s the difference between mutable and immutable?

  • mutable objects : are objects that could be changed like “ list, set, dict”
  • immutable objects: are unchangeable objects like “int, float, bool, str, tuple, unicode”
>>> l1 = [1, 2, 3]
>>> s1 = (4, 5, 6)
>>> print(l1[0])
1
>>> print(s1[0])
4
>>> l1[0] = 100
>>> print(l1[0])
100
>>> s1[0] = 400
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> l1 = [1, 2, 3]
>>> l1 = l2
>>> id(l1)
140180307719752
>>> id(l2)
140180307719752
>>> l1[0] = 100
>>> l1
[100, 2, 3, 4]
>>> id(l1)
140180307719752
>>> id(l2)
140180307719752
>>> l2
[100, 2, 3, 4]
>>> l1 = [1, 2, 3]
>>> id(l1)
140180307719752
>>> l2 = l1[:]
>>> id(l2)
140180307719048
>>> l1[0] = 100
>>> l1
[100, 2, 3]
>>> id(l1)
140180307719752
>>> l2
[1, 2, 3]
>>> id(l2)
140180307719048
>>> l1 = [1, 2, 3]
>>> s1 = (1, 2, 3)
>>> id(l1)
140180340307784
>>> id(s1)
140180347307712
>>> l1 += [4, 5, 6]
>>> s1 += (4, 5, 6)
>>> id(l1)
140180340307784
>>> id(s1)
140180347263592
>>> a = "Test"
>>> b = "Test"
>>> id(a)
140180339994896
>>> id(b)
140180339994896
>>> s1 = (1, 2)
>>> s2 = (1, 2)
>>> id(s1)
140180339957064
>>> id(s2)
140180307779400
>>>
>>> id(a)
10105088
>>> a += 1
>>> id(a)
10105120
>>> a = "Test"
>>> id(a)
140180339994896
>>> a = "Test" + "Test"
>>> id(a)
140180347327024

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

Now we know the difference between mutable and immutable objects we know that immutable objects can be used to make sure that the object remains constant throughout the program. How ever the mutable objects can be changed whether expected or not. Changing a mutable data type won’t change it’s memory address on the other hand changing an immutable type can produce errors.

  • Immutable are quicker to access than mutable objects.
  • Mutable objects are great to use when you need to change the size of the object, example list, dict etc.. Immutables are used when you need to ensure that the object you made will always stay the same.
  • Immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.

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

Its important to know the difference between mutable and immutable types and how they are treated when passed onto functions. Memory efficiency is highly affected when the proper objects are used.

>>> def increment(n):
... n += 1
...
>>> a = 3
>>> increment(a)
>>> a
3
>>> def increment(l):
... l += [4]
...
>>> l = [1, 2, 3]
>>> increment(l)
>>> l
[1, 2, 3, 4]

--

--

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