My Python Learning Journey

B V K
3 min readJul 27, 2023

--

Visual representation of Objects in Python using normal vehicles/cars

Introduction:

As I embarked on my Python learning journey, I stumbled upon some fascinating concepts that significantly impacted how I write code. Objects, values, mutability, and immutability were among the fundamental topics that piqued my interest. Understanding these concepts is vital for writing robust and maintainable Python code. In this blog post, I will share my learning journey, exploring id and type, delving into the world of mutable and immutable objects, and understanding how Python treats them differently. I’ll also discuss how arguments are passed to functions and their implications for mutable and immutable objects.

ID and Type:
At the start of my journey, I discovered that each Python object has a unique identifier (ID) that sets it apart from others in memory. The ID can be accessed using the id() function, while the type() function helps us identify the object’s type. This opened my eyes to how Python manages objects and their underlying data. Let me show you an example:

x = 42

print(id(x)) # Output: 140452709792144

print(type(x)) # Output: <class ‘int’>

Mutable Objects:
The concept of mutability fascinated me, as it allowed certain objects to change their internal state after creation. Lists, dictionaries, and sets are examples of mutable objects. However, this also introduced the concept of aliasing, where two or more variables point to the same object. This led to intriguing scenarios where changes made through one variable affected others. An example that made this concept clear was:

list1 = [1, 2, 3]

list2 = list1 # Aliasing

list2.append(4)

print(list1) # Output: [1, 2, 3, 4]

Immutable Objects:
The notion of immutability was equally captivating, especially with objects like integers, strings, and tuples. Immutable objects, once created, cannot be altered. Any operation that appears to modify them creates a new memory object. I was delighted to explore the implications of immutability on data integrity and the prevention of unexpected side effects. An example of immutability using a tuple is:

tuple1 = (1, 2, 3)

tuple2 = tuple1 # Aliasing

# Attempting to modify the tuple will raise an error

# tuple2[0] = 10 # TypeError: ‘tuple’ object does not support item assignment

Why Does It Matter and How Python Treats Mutable and Immutable Objects Differently:
As I delved deeper into the subject, I began to understand why knowing the mutability of objects mattered so much. Immutable objects ensured data integrity and prevented unintended changes, while mutable objects allowed for in-place modifications. Python treats these objects differently when passing them as function arguments. Immutable objects are effectively pass-by-value, creating new references inside functions. In contrast, mutable objects retain their original reference, leading to some intriguing outcomes. To showcase this behaviour, consider the following example:

def modify_list(lst):

lst.append(5) # Modifying a mutable object

return lst

def modify_number(num):

num += 10 # Attempting to modify an immutable object

return num

my_list = [1, 2, 3]

my_number = 42

modified_list = modify_list(my_list)

modified_number = modify_number(my_number)

print(my_list) # Output: [1, 2, 3, 5] (original list modified)

print(my_number) # Output: 42 (original number unchanged)

In conclusion, my journey through Python’s objects, aliasing, and immutability was both enlightening and rewarding. Understanding these core concepts is vital for writing efficient and bug-free code. By being mindful of aliasing and how Python treats mutable and immutable objects differently, I can now develop more robust and reliable software. I am excited to apply this newfound knowledge in my future Python projects, and I encourage you to do the same! Happy coding!

--

--