Today we are going to talk about objects. Yeah, python is an object oriented programming language and it’s not the only one. To simplify this topic about objects, we are going to answer some question first. What’s an oriented language? What’s an object? And what does Python do different from other languages with objects? Let’s get it started!
What’s an Object Oriented Language?
Firstly, an Object-oriented programming (OOP) is a programming paradigm. That means that every language that is OOP is an example of representative feature. They don’t work in the same way even if those languages use object oriented. So Python will work different that Java, for example. Now, a programming language that is OOP means that is based, for our surprise, in objects. Those objects will contain, and not limited to only one, data and code. The data that can be attributes or properties and code, that will be the methods.
What’s an object?
So now that we understand a little bit about Object-oriented, let’s talk about objects. An object in python can be a variable, a function, a data structure, a method or can be all of them combined. Objects got a particular design pattern and that means that objects can be a reusable solution for many problems within the same context. There’s many types of objects in Python but we are going to limit our time in two of them: mutable and immutable objects.
What is id() and type() in python?
I know that before we said to limit our time in mutable and immutable objects but it will difficult to understand if we don’t talk about id() and type() functions. The id() function is a built-in function of python that shows the unique id of the object. Let see the image below as an example:
Now, to get a better understanding of how id works in python, we can compare them as address of a constant in C. Remember C? So yeah, id in python is something like an address of a constant in C but in this case for python, they use id for the objects. These id can change too, if the object, change. For example, let see the image below:
In this image we can see that the object that variable a changed and so the id changed too. But the id of the variable b didn’t because is still referring to the same object as before, therefore, when you compare them it will give you a False value.
Now, there’s a way to check if these variable refer to the same type of object. Type() is a built-in function that is mostly used for debugging. Now there’s two way to use type(): with one argument, or with three arguments. If we use type() with only one argument we will get a different kind of type that if we use type() with three arguments. The syntax for both of them are like this:
- type(object) -> one argument passed
- type(name, bases, dict) -> three argument passed
In the image below there’s many examples of how type() with one argument works:
Using type() with three arguments will need more time explaining about classes, and objects inside them so for the moment, we are not going to explain how it works but here is more info. of that topic if you’re interested.
Mutable and Immutable Objects
Even the name of these types of objects are different and they can tell you much just by reading them right now. When you create a variable and you instantiated (this means that you’re creating an instance for that object at the time you initiate the variable with an object), the object will get an id (remember when we talked about id?). It’s at that moment when the object will get a type and it can’t be changed afterwards. But, even if this is still true with immutable objects, it will not be with mutable objects. So let’s get into immutable objects first.
These objects cannot change their state or content after being created. We can name some of these types: int, float, bool, string, unicode, tuple.
It’s not hard to understand that they can’t be changed but it’s easy to get if we see an example of it.
As we can see, it will not let us. We can even try this with a string.
Now, mutable are the opposite of immutable. In these objects we can change their state and content, even after being created. It’s the easiest way to change them and is always recommended if there’s a need to change them later on. Now let see an example of it:
We need to get the bigger picture and the best way of this is to understand how arguments are passed to a function and how it affects mutable and immutable objects, if they do.
An argument passed to a function is everything you put inside of a function when you call it. Let say, type(a), we are passing an argument to that function so it can return something to us as result. Now, let see how this works with mutable and immutable because even if we can change the content of an immutable object, it will cost us efficiency in memory by doing it. So that’s why it’s recommended to use mutable instead.
When we pass an argument that is calling by reference a mutable object, for example, changeList(list), where “changeList” is a function that we are calling and “list” is the argument that we are passing, therefore, that function is calling by reference our mutable object “list”. Let see how this works in code below:
Now, this will not work if we send an immutable object as argument. For example:
Here’s another example and this time, is giving us an error just trying to change one of the character of the string:
So both of them got their way to use them. It will always depend on what do you want to do in the future of your program. More of it, there’s an exception with immutable but there’s always one or two in programming. To know more about mutable and immutable you can check this link to understand much more about them. Until then, happy coding!