Python3: Mutable, Immutable... everything is an object!
Camilo Estrada
Software Developer | WordPress | Web Developer | Product Owner | JavaScript | CSS | HTML | React | Python | C | Git & Github | Project Management
Understanding the fundamentals of programming is essential. In this post, we'll delve into some of the key concepts that every developer needs to understand: id, type, mutable objects, immutable objects, and how Python handles these objects.
In Python, every object has an ID, which is a unique identifier for that object, and a type, which defines the characteristics of the object. The ID is assigned when the object is created, and it never changes during the object's lifetime. The type of an object determines its behavior and how it can be used in your code.?
In Python, everything is an object and we can have mutable and immutable objects to use. First, let's talk about mutable objects whose value can be changed after they are created. In Python, mutable objects include lists, dictionaries, sets, and byte arrays. When you modify a mutable object, its ID remains the same.?
On the other hand, we have immutable objects whose value cannot be changed after they are created. In Python, immutable objects include numbers (int, float, and complex), strings, tuples, frozen sets, and bytes. Immutable objects are stored in a fixed location in memory, and their value cannot be changed. When you create an immutable object, Python allocates memory for that object and assigns it an ID. If you create another immutable object with the same value, Python will allocate a new memory location for that object with a different ID. When you modify an immutable object, Python creates a new object with a new ID.?
Now let's see the difference between assignment and referencing variables. When you assign a variable, you create a new object with a new ID. When you reference a variable, you are creating an alias, or another name, for an existing object with an existing ID. This can lead to unexpected results when dealing with mutable objects, as changes made through one variable will be reflected in the other variable. It is important to mention that variables are passed to functions by assignment. When a variable is passed to a function, the value of the variable is assigned to a new variable created by the function. This new variable is a local variable to the function and has the same name as the variable that was passed to the function.
领英推荐
NSMALLPOSINTS and NSMALLNEGINTS:
CPython, the most widely used implementation of Python, pre-allocates a small range of integers from -5 to 256, known as NSMALLPOSINTS and NSMALLNEGINTS. These integers are used frequently in Python programs, so by pre-allocating them, CPython can save memory and improve performance.
Examples:
To illustrate how Python handles mutable and immutable objects, consider the following examples. Suppose we have a list containing two integers: a = [1, 2]. If we modify the list by appending a third integer, a.append(3), the ID of the list remains the same. However, if we have an integer variable, b = 1, and we modify its value by adding 1, b += 1, Python creates a new integer object with a new ID.
Another example is the use of tuples and frozen sets. Both are immutable objects, but they can contain mutable objects such as lists or dictionaries. In this case, the ID of the immutable object remains the same, but the ID of the mutable object changes when modified.
In summary, understanding the basics of Python objects, their types, and their mutability is crucial for every software development student. Knowing the difference between mutable and immutable objects, and how Python handles them, can help you write more efficient and robust code. Remember to pay attention to how objects are passed as arguments to functions and to consider memory allocation when dealing with mutable objects. Additionally, understanding the difference between assignment and referencing, as well as the pre-allocation of NSMALLPOSINTS and NSMALLNEGINTS, can further improve your Python programming skills.