Mutable, Immutable... everything is object!
Welcome into the world of objects in python! In this blog post, we'll explore some fundamentals concepts related to objects, such as id and type, and understand the distinction between mutable and immutable objects. We'll see why this distinction matters and how python treats these objects differently. Also we'll find out how arguments are passed to functions and the implications for mutable and immutable objects.
ID and Type:
In Python, every object has a unique identifier (id) that represents its memory address. The id function allows us to obtain this identifier. Similarly, the type function provides information about the type of an object. Let's find out how:
a = 4
print(id(a))? # Output: 140482508851920
print(type(a))? # Output: <class 'int'>
Mutable objects:
Mutable objects in Python can be modified after they are created, means the value of a mutable object can change while it's memory location remains the same, but how it works ?
my_list = [1, 2, 3]
print(id(my_list))? # Output: 140482526186496
my_list.append(4)
print(my_list)? # Output: [1, 2, 3, 4]
print(id(my_list))? # Output: 140482526186496 (Same ID as before)
领英推è
Immutable objects:
This type of objects can't be modified after they are created. Any operation that appears to modify an immutable object actually creates a new object. We can list integers, floats, strings and tuples. Let's see this exemple:
my_string = "Hello"
print(id(my_string))? # Output: 140482560024560
my_string += " World"
print(my_string)? # Output: Hello World
print(id(my_string))? # Output: 140482559899184 (Different ID than before)
Why Does It Matter and How Python Treats Mutable and Immutable Objects Differently:
Understanding the distinction between mutable and immutable objects is crucial in Python as it affects the behavior and performance of our programs. Mutable objects allow in-place modifications, which can be efficient for large data structures. However, they require caution when dealing with shared references and unexpected side effects. On the other hand, immutable objects provide immutability guarantees, making code more predictable and robust. They ensure that objects remain unchanged once created, facilitating reasoning about program behavior and enabling certain optimizations. By treating mutable and immutable objects differently, Python strikes a balance between flexibility and safety in programming.
How Arguments Are Passed to Functions and Implications for Mutable and Immutable Objects:
In Python, function arguments are passed by assignment, which has implications for mutable and immutable objects. When a mutable object is passed as an argument and modified within the function, the changes persist outside the function as well. This behavior can be convenient for modifying data structures. However, immutable objects, such as strings and tuples, are passed by value. Any modifications made within the function create new objects, leaving the original objects unaffected. This behavior ensures that immutable objects remain unchanged and preserves their desired immutability guarantees. By understanding how arguments are passed, we can write more reliable and maintainable code while leveraging the benefits of both mutable and immutable objects. See the exemple below:
def modify_list(lst):
? ? lst.append(4)
my_list = [1, 2, 3]
modify_list(my_list)
print(my_list)? # Output: [1, 2, 3, 4]