Programming Terms: Mutable vs Immutable

preview_player
Показать описание
In this programming terms video, we will be going over the difference between mutable and immutable objects. An immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. Let's take a look at some examples as to what exactly this means and why it is important to know.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
Рекомендации по теме
Комментарии
Автор

Man, have watched thousands of video's on YouTube. But you are the best . No one explains better than you. I'm glad I I found your channel.

matuagify
Автор

In your last example of displaying the employees, you said its a bad way to represent a big list of employees, so what would be the right way ?

markrity
Автор

I already had a good grasp of mutable and immutable, but I'll admit; your explanation definitely made the distinction clear. Especially with regards to how immutable objects occupy a distinct space of memory and how trying to modify them can create multiple objects in memory. Overall, I learned a lot so thank you very much for posting these. :)

Avarn
Автор

This video feels like finding gold among the archives. Though the tools are outdated (python 2) but the concept remains a timeless asset.

waiitwhaat
Автор

Thank you Corey for your time and effort. Keep spreading the knowledge.

soumyazyx
Автор

The difference between mutable and immutable objects is very nicely explained in the first half of the video. You are absolutely correct that an application which keeps appending value to an immutable object runs slow as it constantly copies the existing value to a new memory space. However, there is no guarantee that by changing it to a mutable object the application will run fast. Using list, it is indeed the case as list in Python is essentially a variable-sized array which has extra spaces to add new elements. So its operation to append is "amortized" O(1). But if one uses numpy array, then it is essentially the same as using string which copies the existing elements over to a new memory location whenever new elements are appended (i.e. O(N) operation).

YeGaogaosanyong
Автор

I wish I could give you more than one thumbs up. Your explanation and example really hit the concept home for me. I hope you can get more views since I think you did a very good job. The final example was a great practical example of how the way it is stored in memory could really affect what you are trying to do - in certain situations mutable is ideal in others immutable will have great advantages. The better you understand the difference between the two the better you can decide the approach to best meet your needs.

psalt
Автор

Summary:
1. 'Mutable' means that an object can be modified, while 'immutable means it can't.


2. We can check whether a data is mutable or not by printing out the id(memory address) of an object after performing an modification to the data type.


3. Example#1. Strings are immutable in Python. But we can still reassign a whole new string value oto a variable that holds a string. However, it is not possible to modify a substring while keeping the memory address the same.


4. Example#2. Lists are mutable in Python. We can change one item of the list while keeping the memory address of the entire list the same.


5. Why should we know this concept? There are 2 reasons
5-1 We can avoid and fix errors caused by modifying an immutable data type.
5-2 We can speed up our programs. Memories are being shifted when performing operations on immutable objects means that it is going to take a lot more amount of time, and making our program slower. By avoiding operations on immutable data and thus the memory shift, we can improve our program speed.

xcxfgpn
Автор

By far the best explanation of immutability for beginners. Thanks!

alishashrestha
Автор

Hi Corey, thanks for this video. Well the String and StringBuffer example was comprehensible. You made it very clear that having to concatenate 1000's of string would take a performance hit and it would be highly memory inefficient to do that. But you forgot to state what would be the memory efficient way to approach this problem. What would be the StringBuffer/StringBuilder equivalent Java solution in Python?

think.force-anil
Автор

thanks for the video, I wanted to understands mutable vs immutable in terms of JavaScript, and your example really helped me out to understand it

TheLollercaster
Автор

Great Videos, thank you!! One question: The different addresses are the addresses to which output pointed in the past. However, after each concatenation the old object gets garbage collected. So where is the problem? All the time, there is just one object, that moves from one location in memory to another, letting the old objects be destroyed...

mickmack
Автор

+1 for clarity and choice of example with the String class at the beggining :) thank you!

FatDada
Автор

Thank you so much for your generous time and effort!

potatobot
Автор

This is the greatest explanation on this top! Can you give more examples when using mutable and immutable variables are good or bad when processing large amounts of data?

MarcusLucasOficial
Автор

Great Explination 5 articles = 1 Video

kollivenkatamadhukar
Автор

Thanks so much for explaining why its important to know which objects are mutable and which are not.

self.medicate
Автор

Corey you really push the fog out. Thanks very much.

RC-blpm
Автор

best video to explain the concept . Thank you

pinkfloyd
Автор

Adding one more point, immutability makes the data thread safe.

ankitjhunjhunwala