Python3: Mutable, Immutable... everything is Object!
Introduction:
Python, a versatile and powerful programming language, employs the concept of mutable and immutable objects. In this blog post, we will explore the significance of mutable and immutable objects, the differences between them, and how Python treats them in various scenarios. Understanding these concepts is essential for writing efficient and bug-free Python code.
ID and Type:
Every object in Python has a unique identity represented by its ID. The id() function returns the identity of an object, which corresponds to its memory address. Additionally, objects have a type that determines their behavior and the operations that can be performed on them. The type() function provides the type of an object, allowing us to identify whether it is mutable or immutable.
Exemples:
a= 2
print("id of a = {}.".format(id(a)) )
print("type of a is {}.".format(type(a)) )
output:
id of a = 123252344545734.
type of a is int.3
Mutable Objects:
Mutable objects in Python, means that their state or contents can change. For example, appending elements to a list or updating the values in a dictionary are valid operations on mutable objects. The key characteristic of mutable objects is that they have a changing state, and multiple variables can reference the same underlying object.
Here are the objects in Python that are of mutable type:
Exemples :
my_list = [1, 2, 3
print(my_list) # Output: [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
my_list[0] = 5
print(my_list) # Output: [5, 2, 3, 4]]
Immutable Objects:
On the other hand, immutable objects in Python, cannot be modified once they are created. Immutable objects have a fixed state, and any operation that seems to modify them actually creates a new object. For instance, concatenating two strings or performing arithmetic operations on numbers result in new objects rather than modifying the existing ones.
领英推荐
Here are the objects in Python that are of immutable type:
Exemples:
a = "Hello"
a +=" World"
print a
Output
"Hello World"
What are?NSMALLPOSINTS,?NSMALLNEGINTS and reasons are used for ?
NSMALLPOSINTS and NSMALLNEGINTS are constants in Python that represent pre-allocated small integer objects. These objects are created and cached by the interpreter for efficiency purposes.
In CPython (the reference implementation of Python), small integers ranging from -5 to 256 are pre-allocated and shared among different parts of the program. This means that when you use these small integers in your code, you're actually referencing the same objects in memory rather than creating new objects every time.
For example, if you write x = 10 multiple times in your code, all those instances of 10 will refer to the same pre-allocated object, as long as the value is within the range of -5 to 256.
This caching mechanism helps improve performance and saves memory by reusing these commonly used small integer objects instead of creating new objects every time they are needed. However, it's important to note that this behavior may vary across different Python implementations or versions, so it's not guaranteed to be present in all cases.
Importance and Treatment of Mutable and Immutable Objects in Python:
Understanding the distinction between mutable and immutable objects is crucial because it affects how Python handles assignments, variable references, and function arguments. Python treats mutable and immutable objects differently in terms of memory management, performance, and behavior. Immutable objects are preferred when data integrity and immutability are desired, while mutable objects offer flexibility and efficiency in scenarios requiring frequent modifications.
Argument Passing and its Implications for Mutable and Immutable Objects:
In Python, arguments are passed to functions using a combination of pass-by-value and pass-by-reference mechanisms. Immutable objects are passed by value, meaning that the value of the object is copied and passed to the function. Thus, any modifications made to the object within the function only affect the local copy, leaving the original object unchanged. On the other hand, mutable objects are passed by reference, enabling modifications made within the function to affect the original object itself.
Conclusion:
In conclusion, understanding mutable and immutable objects is essential for writing robust and efficient Python code. The distinction between mutable and immutable objects affects how they are treated by Python in terms of memory management, performance, and behavior. Recognizing when to use each type of object is crucial for data integrity and code optimization. Additionally, understanding how Python passes arguments to functions provides insights into how modifications to mutable and immutable objects behave within function scopes. By mastering these concepts, developers can harness the power of Python and write code that is both elegant and effective.