Mutable, Immutable... everything is object!

Mutable, Immutable... everything is object!

Let's suppose that you want to change something on your body. You can change color of your hair, quantity or style of your hair ;) color of your skin, length of your eyelashes, nails and so much more and if this would be our only characteristics, we definitely would be mutable objects, that you can change the parts whenever you want. BUT what makes us immutable is our DNA, iris, fingerprints, voice, specific characteristics of our body. So if we are going to think about our body as a class, the objects of our body are going to be divided as mutable object and immutable objects what would that be? You can leave me a comment about that below.

Object and class are two very important components of Python that makes python immutable from the way how specifically operates with this objects but mutable on the class of Object Oriented Programming Languages.

Two very important specs of an object in python, that are even functions, are id() and type().

id() is a function that in Python returns the identity of object. It is the address of object in memory and It will be unique and constant throughout the lifetime of object. This is an immutable characteristic of the object in Python.

type() is a function in Python that returns the type of the specified object. So if we bring this again as it happens to our body, the organs of our body are divided in 11 systems from which if for example we say type(mouth) (if we consider mouth as an object, it will return body organ, component of digestive system) this will return us a different output from type(Mouth, Esophagus, Liver, Stomach, Small Intestine, Large Intestine, Rectum, Anus)(this will return digestive system). Also if we say from the list of organs one or more from the organs of the list above the type() won't return the same output. It is very important to remember that once you set the type() of an object you can not change it! I hope you were good at biology :D .

So why it is so important and necessary to know all this?

There are 2 main reasons why this are important regarding geeksforgeeks.com:

  1. Improves the Exactness and simplicity?of the whole code, as it provides an ease to the coder to pass around the object in the program without any fear as it never seems to be ever modified. whereas the mutable is much harder to reason about. In mutable, the aliasing leads to many irregularities and eventually intimidating fidelity of the code, At the end resulting in variability.
  2. Thread Safe?– As an object after the assignment cannot be modified, this deduces that a read-only data is being shared among the threads, which certainly provides thread-safety. In simpler words, In immutables, since there is no scope of change for an object, there is no need to be scared of accessing it from many threads. Mutation in any sense in immutable objects can be achieved in such a way that one creates a new object instead of trying to modify existing ones.

All of these information above is in our interest to know and keep in mind as it is very important for all python developers and software engineers to make an efficient and fast code. Its important to know the difference between mutable and immutable types and how they are treated when passed onto functions. Memory efficiency is highly affected when the proper objects are used. For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.

I hope that this article will clear your mind if you just stepped in Python or programming, especially if you come from a different background like me and want to learn how to code.

If you are not that clear, or if there is something you would like to suggest, please feel free to write a comment below so as in coding we improve this article and understanding. Thank you for taking time to read this article. See you soon!

要查看或添加评论,请登录

Eno Gupe的更多文章

  • Class and instance attributes

    Class and instance attributes

    Hello my readers! I would like to thank you upfront for your time while reading this new article. Today I will write…

  • How to list files in a very efficient way?!

    How to list files in a very efficient way?!

    What does the ls *.c instruction do at the conceptual level? (regardless of the language used) Lists the files in the…

    1 条评论
  • Static library in C

    Static library in C

    A library is a collection of code routines (functions, classes, variables, and so on) that can be called upon when…

社区洞察

其他会员也浏览了