PYTHON…MUTABLE VS IMMUTABLE OBJECTS
Betty MUKANKUSI
Software Engineering student at African Leadership University | UX/UI Designer | Fashion enthusiast | Travel enthusiast
Sharing my takeaways in Python, Everything is an object, I will talk about id and type functions, mutable and immutable objects, and how differently Python treats mutable and immutable objects. Lastly, I will discuss how arguments are passed to functions and what it implies for mutable and immutable objects.
id and type functions
The id() function in Python returns the identity of a specified object. Every object created in Python has a unique id assigned to it as it is stored on the memory and is also classified into specific data type.
The following two examples illustrate a variable with different values.
>>> x = 23
>>>print(id(x))
>>>print(type(x))
# Output
140715606074856
<class 'int'>
# second example
>>>x = “Hello, World!”
>>>print(id(x))
>>>print(type(x))
# Output
1332602695088
<class 'str'>
Mutable vs Immutable objects
Mutable objects are objects that can be changed after they have been created. Examples include lists, sets, and dictionaries.
Example:?
>>>a_lst = [1, 3, 5, 7, 9]
>>>a_lst[1] = 8
>>>print(a_lst)
[1, 8, 5, 7, 9]
Immutable objects are objects that can't be modified after they have been created, for example, strings and tuples.
Example:
>>>my_str = “This can not be modified”
>>>my_str[5:8] = “will”
>>>print(my_str)
TypeError: 'str' does not support item assignment on line 2
Why does it matter, and how differently does Python treat mutable and immutable objects
The difference in mutable and immutable objects matters in Python because it affects how they are stored, copied, and used. Since mutable objects can be changed, they occupy different locations, hence different id. But an immutable object’s id remains the same even if its values are changed.?
领英推荐
Example:
>>>a = "apple"
>>>b = "apple"
>>>print(a==b)
True
>>>print(a is b)
True
Because strings are immutable, Python makes two names with the same string value to refer to the same object. But this is not the case for lists that are mutable:
>>> a = [1, 2, 3]
>>>b = [1, 2, 3]
>>>print(a == b)
True
?>>>print(a is b)
False
a and b have the same value but do not refer to the same object.
How are arguments passed to functions, and what does that imply for mutable and immutable objects?
Mutable and immutable objects behave differently when passed to a function. When mutable functions are passed to a function and changed within, the changes persist outside the function also. While immutable objects passed to a function can not be changed within the function, meaning a new object is created, and any changes made remain within the scope of the function.
Example:?
>>>def modify_list(lst):
?>>>lst.append(5)
?>>>?print(lst)
>>>my_list = [1, 2, 3]
>>>modify_list(my_list)? # Output: [1, 2, 3, 5]
>>>print(my_list)??? ?# Output: [1, 2, 3, 5] - 'my_list' is modified
>>>def modify_integer(num):
>>>num = 100
>>>print(num)
>>>x = 42
>>>modify_integer(x)? # Output: 100
>>>print(x)????? ?# Output: 42 - 'x' remains unchanged
Python is great…happy coding! #codinglife #python #Pythonobjects