Python3: Mutable, Immutable… everything is object!

Object Oriented Programming

Object

Class

>>> isinstance(1, object)
True
>>> isinstance(False, object)
True
def my_func():
return "hello"
>>> isinstance(my_func, object)
True
Standard compound data types

Type

>>> x = 42 
>>> typex
<class 'int'>
>>> y = 24.5
>>> type(y)
<class 'float'>
>>> def f(x):
... return (x+1)
...
>>> type(f)
<class 'function'>
>>> import math
>>> type(math)
<class 'module'>

Identity

>>> x = 1
>>> id(x)
10105088
>>> x = "Holberton"
>>> y = "Holberton"
>>> x == y
True
>>> x = "Holberton School"
>>> y = "Holberton School"
>>> x == y
True
>>> id(x)
139798528064800
>>> id(y)
139798528064872
>>> id(x) == id(y)
False
>>> x == y
True
>>> id(x) == id(y)
False
>>> x is y
False
>>> x = "Holberton School"
>>> z = x
>>> id(x)
139798528064944
>>> id(z)
139798528064944
>>> x == z
True

Mutable and Immutable Objects

Immutable objects

>>> x = 37598
>>> x
37598
>>> x = 37599
>>> x
37599
>>> x = 37598
>>> x
37598
>>> id(x)
139798529290128
>>> x = 37599
>>> x
37599
>>> id(x)
139798528694160
>>> my_tuple = (1, 2, 3)
>>> id(my_tuple)
139798528063096
>>> my_tuple = (3, 4, 5)
>>> id(my_tuple)
139798528064608
>>> my_tuple[0] = 'a new value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Mutable Objects

>>> my_list = [1, 2, 3]
>>> my_list[0] = 'new value'
>>> my_list
['new value', 2, 3]
>>> my_list = [1, 2, 3]
>>> id(my_list)
139798488481416
>>> my_list
[1, 2, 3]
>>> my_list[0] = 'new value'
>>> id(my_list)
139798488481416
>>> my_list
['new value', 2, 3]
>>> x = y = [1, 2]
>>> x is y
True
>>> id(x)
139798488480520
>>> id(y)
139798488480520
>>> id(x) == id(y)
True
>>> x.append(3)
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
>>> id(x)
139798488480520
>>> id(y)
139798488480520

Why is it important and how does Python handle mutable and immutable objects?

>>> my_list = [(1, 1), 2, 3]
>>> my_tuple = ([1, 1], 2, 3)
>>> type(my_list)
<class 'list'>
>>> type(my_list[0])
<class 'tuple'>
>>> type(my_tuple)
<class 'tuple'>
>>> type(my_tuple[0])
<class 'list'>
>>> my_list[0][0] = 'Changed!'    (1)
>>> my_tuple[0][0] = 'Changed!' (2)
>>> my_list[0][0] = 'Changed!'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> my_tuple = ([1, 1], 2, 3)
>>> id(my_tuple)
139798528063096
>>> type(my_tuple[0])
<class 'list'>
>>> id(my_tuple[0])
139798520723336
>>> my_tuple[0][0] = 'Changed!'
>>> id(my_tuple)
139798528063096
>>> id(my_tuple[0])
139798520723336
>>> my_tuple
(['Changed!', 1], 2, 3)

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

>>> def increment(n):
... n += 1
...
>>> a = 9
>>> increment(a)
>>> a
9

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

Preallocation in Python

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