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:
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!