Python3: Mutable, Immutable... everything is object!

Python3: Mutable, Immutable... everything is object!

Objects are the equivalent to "Things" in programming. In general, a Thing has a name, it has attributes/properties, and you can do stuff with it. The object equivalent of those are the class name, class properties/attributes, and methods (which are basically just functions inside objects, unless you want to be really precise).

Objects are a pretty intuitive way of grouping Things together that are similar, by defining a template of the generalized concept of the Thing and its associated attributes/methods.

Now, you can take this concept of defining something like this to the logical conclusion that everything you can imagine, anything you can name, fits this schema. This is pretty much what python did. Under the hood, most aspects of the language itself work like objects. They belong to a class, which has a name, and they have certain operations pre-defined that you can do with/to them. Notable examples that I can think of are None and small integers as well as Type, but I might even be wrong on those, so don't quote me on it.

  • Object-oriented programming is a way of thinking, a method to model reality and make the code easier to structure/read/reason about. Read the wikipedia article comparing it to procedural/functional programming and you will see it is just the logical next step in abstraction after those.

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code.

- Id and type.

No hay texto alternativo para esta imagen

When you create an object in python automatically is assigned an Id to that object, and exist a way to get the id of and specific object.

Python is using a pure object model where classes are instances of a meta-class “type” (in Python, the terms “type” and “class” are synonyms). And “type” is the only class which is an instance of itself

No hay texto alternativo para esta imagen

Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created.

No hay texto alternativo para esta imagen

Mutable Objects : These are of type list, dict, sets . Custom classes are generally mutable. We can see that when we try to change the tuple we get an error, but we don’t have that problem with the list.

How Python treats mutable and immutable objects differently?

Understand completely that concept make the difference, isn't just type code and finish a task, Is about know what happend under the hood, Sometimes , when our code has a stranges behaviors , We don't know how handle it , but precisely, That's the reason. If you know that , you could do more efficient code.

Imagine that you the a tuple, something like this:

Tuple1 = (1, 2, 3, [1, 2, 3])

In that case, the tuple, definitely is a immutable object, but what about the LIST inside the tuple ? the list is a mutable object, therefore I could change the list inside the tuple.

How arguments are passed to functions and what does that imply for mutable and immutable objects?

There is an excellent article written by Luciano Ramalho in which he explains how to understand variables in Python. I am not going to copy his article, but I think it is a great inspiration on how to explain things. What he suggests is to think about labels and not about boxes when referring to variables. A variable is a label that we assign to an object, it is the way we, as humans, have to identify it. However, what is important about the underlying object is its value and its type.

A great tool in Python to understand this concept is the id function. We can apply it to any variable and it will return its identity. If we want to be sure about dealing with the same object, we can check whether the value returned by id is the same. It is possible to think about the integer that is being returned as the address in memory that is assigned to the object. So, for example, we can do the following:

>>> var1 = [1, 2, 3]
>>> var2 = (1, 2, 3)
>>> id(var1)
44045192
>>> id(ar2)
43989032

As you can see, python assign different id to each of them, because are direfents objets

Tuples are not the only immutable data type in Python, but they are a great tool to learn because they can be directly compared to lists, which are mutable. Other immutable data types are

No hay texto alternativo para esta imagen

Most likely you haven't thought about it before, but when you assign an integer, float, etc. to a variable, it can't be replaced. So for example, you will get an output like this if you check the identity of an integer assigned to a variable

>>> var1 = 1
>>> id(var1)
1644063776
>>> var1 += 1
>>> id(var1)
1644063808

As you can see, when you change the value to a var1, a new object is created with a new Id

The same would happen with all the other data types listed above. Mutable objects, on the other hand, are the following:

list, dictionary, set, bytearray and user defined classes

Those are the kind of objects that can be changed in-place, without creating a new one to store the updated values. An interesting case happens when you give two names to the same variable, for example:

>>> var1 = [0, 1, 2]
>>> var2 = var1
>>> id(var1)
44372872
>>> id(var2)
44372872
>>> var1 += [3, 4, 5]
>>> print(var2)
[0, 1, 2, 3, 4, 5]
>>> var1 is var2
True


To view or add a comment, sign in

More articles by Juan Alberto Londoño

  • The public transport service review app.

    Picture the following situation: you or a loved one are about to board a taxi. Many questions arise such as: is it…

  • How to perform automated data augmentation.

    The prediction accuracy of the Supervised Deep Learning models is largely reliant on the amount and the diversity of…

  • BTC - Time Series Forecasting

    This site is still being built, be patient and come back soon :D What Time series forecasting is? Time series…

  • Dynamic libraries in C

    In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program. Libraries…

  • C static libraries

    Why use libraries Generally the programmers are organized peoples, and if we have somethings in common is that we don't…

  • What is the difference between a hard link and a symbolic link?

    links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file.

  • Using ls *.c in Linux, Like eating cookies (easy)

    Linux is Os like any other Os, some of its distributions like, Fedora, Ubuntu, Manjaro and others, has friendly, fluid…

Others also viewed

Explore topics