Python Forum
What is the meaning of mutable data type?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the meaning of mutable data type?
#1
Hello everyone, I have one simple question which I can not figure out by myself. Would you please share your opinion and help me walking through this?
The question is: what is the meaning of making the difference between mutable and immutable data type in python? Could anyone please tell me what kind of benefits the mutable data type could offer comparing to the immutable data type, maybe give me an example?
Thanks.
Reply
#2
mutable
Mutable objects can change their value but keep their id().

immutable
An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.
Reply
#3
Mutable = can change

Immutable = can't change.

Eg =

A list is mutable as I can add/remove elements.

A tuple is immutable as once I declare it, I can't add/remove any elements.
Reply
#4
The first time I ran across immutable strings I found it very confusing, but that was coming from a world were there was no garbage collection unless I did it myself. In C you create/protect/cherish your data structures because they are the foundation of your program. In Python they are tossed away like a used tissue.

But I have to say there are some nice things about immutable strings. I don't have to worry about a string changing value in python. In C, if I kept a string I usually made a copy to protect against the string changing outside the scope of my module. Python does that automatically, and only if I need a copy. If the string I am passed never changes it can be used all over the place and still be the only string. No copies unless needed and then the copies are made automatically. How cool is that!

To a lesser extent I like tuples for the same reason. If I put something in a tuple I don't have to worry about it changing. If I want something that acts more like a C array (a shared resource that can be modified and all users have access to the same modified value) I just use a list (or a python array).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 268 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  mutable argument in function definition akbarza 1 423 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Csv writer meaning of quoting mg24 2 1,105 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  mutable values to string items? fozz 15 2,696 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 1,877 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Operator meaning explanation Sherine 3 1,980 Jul-31-2021, 11:05 AM
Last Post: Sherine
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,911 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,253 Jan-30-2021, 07:11 AM
Last Post: alloydog
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 6,718 Dec-25-2020, 02:31 AM
Last Post: tinh
  parser.parse_args() meaning vinci 2 2,549 Oct-26-2020, 04:13 PM
Last Post: vinci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020