Sitemap

Python3 Mutable, Immutable… everything is an object!

Introduction:

--

Understanding how Python handles data can help your programming skills. This blog post will

talk about fundamental concepts, such as Python’s handling of mutable and immutable objects,

and how this impacts functions and arguments. By the end, you’ll have a clearer understanding

Get Gabriel Rivera’s stories in your inbox

Join Medium for free to get updates from this writer.

of how these concepts affect your code and how to use them effectively.

Press enter or click to view image in full size

ID and Type

In Python, every object has an identity, a type, and a value. The identity refers to the object’s memory location, which is constant during its lifetime. You can check an object’s identity using the id() function. The type tells you what kind of object it is, which can be found using the type() function. These two function are important to distinguish between mutable and immutable objects.

x = 10
print(id(x)) # Unique identifier for the object
print(type(x)) # Type of the object: <class 'int'>

Mutable Objects

Mutable objects are those that can be changed after they are created. Mutable objects in Python include lists, dictionaries, and sets. When you modify a mutable object, its identity (or memory location) remains the same, but its value changes. This makes mutable objects very flexible, but they also require careful handling to avoid unintended side effects.

my_list = [1, 2, 3]
print(id(my_list)) # Memory location of the list

my_list.append(4)
print(id(my_list)) # Memory location remains the same
print(my_list) # The list value is updated: [1, 2, 3, 4]

Immutable Objects

Immutable objects, on the other hand, cannot be changed once they are created. Immutable objects in Python includes integers, floats, strings, and tuples. Any operation that modifies an immutable object will create a new object with a different identity. Understanding immutability is important for writing better code.

my_str = "Hello"
print(id(my_str)) # Memory location of the string

my_str += " World"
print(id(my_str)) # New memory location
print(my_str) # The string is updated: "Hello World"

Why Does It Matter?

The distinction between mutable and immutable objects affects how Python handles data. Python treats mutable and immutable objects differently in memory management. Mutable objects can lead to unexpected bugs if not handled properly, especially when passed to functions. Immutable objects, being constant, offer safer and more predictable behavior.

How Arguments Are Passed to Functions

Python’s handling of arguments can be tricky. When you pass mutable objects to a function, any modifications within the function affect the original object. With immutable objects, the function operates on a copy, leaving the original unchanged. This behavior is essential to understand to avoid unintended side effects.

def modify_list(lst):
lst.append(4)

my_list = [1, 2, 3]
modify_list(my_list)
print(my_list) # The original list is modified: [1, 2, 3, 4]

def modify_string(s):
s += " World"

my_str = "Hello"
modify_string(my_str)
print(my_str) # The original string is unchanged: "Hello"

Conclusion

Grasping the concepts of mutable and immutable objects, as well as Python’s handling of function arguments, is essential for any Python programmer. These principles will help you write more predictable and efficient code, avoiding common pitfalls related to data handling.

--

--