Python3: Mutable, Immutable…Everything is an Object!
So here we go. Into the world of Python. Perhaps you are coming from another language, and if you’re new to Python, you may find it interesting that everything is an object. Yes! In any case, I’m going to describe a few things I have picked up on my Python journey focusing on the concepts of mutable and immutability concerning objects.
First of all, what is an object? It depends on what language you are using since different programming languages define “object” in different ways. In Python everything is an object in that objects can be pass as arguments into functions or assigned to variables. Some objects may have attributes and methods while others do not (more on this later). This idea that everything is an object implies that variables, lists, tuples, sets, dictionaries, even modules are all objects!
All this can be collected into what is commonly known as object-oriented programming (OOP). Attributes associated with objects are fields that contain data and methods are functions associated with objects. Another important concept in OOP is classes, which can be thought of as a blueprint or template for the object created. This is where objects get their variables and functions (attributes and methods). Other objects can make use of this is template and at that point they are referred to as instances. Perhaps a little confusing to think of all at once, but here is info graphic to visualize this:
Here we have a car object with various attributes and possible methods. Some immediate attributes that are visible are the car brands and colors. Some methods that be in the car class is honkHorn, moveForward, etc. A general blueprint/class could be used as such:
type and id
We just went through some basic concepts surrounding objects. In Python, there are few built-in functions that directly work hand-in-hand in with objects. type returns the type of an object. id returns a unique and constant integer for an object, and some objects may have the same id while others may not. Here are a few examples:
We see that type( ) does indeed let us know what class that object belongs to and that id( ) shows us integer associated with the object of interest. Here are a couple interesting things we notice though from id( )
When the variable “a” has a string value “hi” and the variable “b” also has the string value “hi”, they both have the same id! However, when the ids “a” and “b” are compared when they have the same list value, their ids don’t match! This is do to strings being immutable and lists being mutable — both very important concepts concerning OOP and discussed next.
Mutable objects simply refer to objects that can be an altered. As seen with a list above. This is important since having the option to change a list for example may save space as opposed to having to create new list every time. We see this when we add values to the list “a”.
Above we see that when use append and += with “a” we are adding a value to the object “a”. However, that last method of addition involved setting assigning a new set of memory for the object “a”, [1, 2, 3, 4, 4] and adding 5 to it. This can present an inefficiency in memory allocation but may be useful in other circumstances. Additionally we have to be careful with the last method of addition in situations involving 2 variables referring to the same variable as shown below:
We see that initially “a” and “b” are referring to the same list object, but when 5 is added to “a” as shown, “a” is actually be reassigned to a new object that is a combination of the “a” and . Awareness should be exercised here when more advanced cases arise involving custom objects and class created.
The following are mutable objects:
- user-defined classes (unless specifically made immutable)
Immutable is of course of the opposite of mutable meaning that the objects cannot be altered in this state. Above we saw the id for both “a” and “b” are the same since they are both referring to the “hi” string object. Some immutable objects are:
A great analogy view immutable objects in some cases is to think of them as a tag for referring to the same object or perhaps a different object. As outline in a great post (http://radar.oreilly.com/2014/10/python-tuples-immutable-but-potentially-changing.html) suppose there are 2 tuples:
Clearly “a” and “b” refer to different objects that have same value and “==” evaluates to True due to this, however, using “is” shows that the two objects are not the same.
Mutable vs Immutable
As mentioned before the importance of mutability may be important when it comes to memory. Instead of having to constantly recreated string in memory when you are concatenating a string, an efficient method may be to appending strings with one another within a list. On the other hand, if you wanted to use data that is unchangeable and can repeatedly be used over, using immutable objects may be the better choice. This avoids having to check if any data has been changed.
Passing Arguments to Mutable and Immutable Objects
Finally, we will examine another interest situation with objects. When “a” is passed into the increment function as an argument it still retains it value since “a” is equated to an int which is immutable and the function does not return a value that is reassigned to “a”. Therefore, “a” is still equal to after being used as an argument in the increment function.
In the next example we see a list passed as an argument and the 4 is appended! This happens since the append method reference the list in memory and appends 4 to that list. The object is same and changed (thus mutable).
However, we see that the when the function below is setup to have the first argument that being “l1” reassigned to “l2”, “l1” retains its value since reassignment does not happen in memory, and “l1” is not reassigned to the result from having that returned from the function.
This summarizes a few things about objects in Python involving mutability and immutability, but there is much more to see in larger sets of code. Happy coding!