Mutable, Immutable... everything is object!
Rachael Bradford
Full-Stack Web Developer/ Software Engineer | Next.js, Three.js, SaaS Development,React, Typescript, HTML
Python is a high level, general purpose, interpreted object-oriented programming language. Within the Python realm, you'll find essential elements like objects and built-in functions.
?Today, let's take a closer look at two of these functions, id() and type(), and explore the fascinating world of mutable and immutable objects.
All in all, the id() returns the id of an object short and simple. ?Think of the id() function as a tool that hands you a unique identification card for your object. It provides the precise memory address where your object resides. This address might change across devices or due to memory allocation adjustments.
The type() function returns the type of the specified object. In Python, you don't need to explicitly declare data types as you do in languages like C. Python automatically keeps track of this information. The type() function comes in handy by revealing the data type of a specified object. It's like having an information tracker that informs you about the object's type.
?We keep discussing objects and that is because in Python everything is an object. In fact, the built-in functions we were just talking about are characteristics of an object. So, remember that in Python, every piece of data is treated as an object.
?This is important because in Python objects can be divided into two primary categories: mutable and immutable. This will affect how objects are modified and passed between functions. Some examples of mutable objects are lists, dictionaries, set, and bytearrays. You can modify their contents after they're created, giving you flexibility when working with data. Some examples of immutable objects are integers, floats, strings, tuples, Booleans, complex, NoneType, and frozenset. Once created, they cannot be changed. This unchanging nature ensures that your original data remains intact.
领英推è
Each electronic device we use every day has been programmed to perform a task. When it was developed it was determined what this object would look like and what it would be able to do. From there was a path created to reach that outcome. If you are still following me, thus mutable and immutable objects can be broken down this way. It is set mutable objects can be changed and immutable objects cannot after creation. So, seeing your desired outcome you would want to choose from that perspective.
I read this somewhere once and wrote it down. It was “Immutable objects offer safety and predictability while mutable objects provide efficiency and in-place modification capabilities.†I really enjoyed it because it helped me understand the difference and yet the importance of both functions. I felt it is a great takeaway.
You can pass arguments to functions too. This is calling a reference to the object being summoned. Now, let's talk about passing arguments to functions. Think of it as summoning an object and saying, "Let's work together!" However, there's a twist with mutable objects, changes made inside the function can directly impact the original object. It's like collaborating in a shared workspace. When you make changes, everyone sees them. However, an immutable object is more guarded like it’s like when you’re creating music as an artist you keep your original file so that you always have backup if you don’t like the changes, you can always give it a go from scratch again. So, the same when changes are made, a new object is created, leaving the original untouched.
Thank you for reading please let me know your thoughts.
?
Thank you for reading please let me know your thoughts.