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. :)
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).
+Mark Davydov It really depends on the scenario for how you would display the list. In the example in the video, the main thing to take away was that the memory address was changing with each iteration. So instead of appending to an output string at each step, we could have instead appended to a result list and then printed out that list after the iteration was complete. And if you only need a simple delimiter when displaying a list, you could use join, e.g. '-'.join(employees) So there are several approaches to doing it the correct way based on what you need at the time, but this was more meant to show why this approach wouldn't be the best in this scenario.
@@coreyms Great video, Corey, but i have a question: @ 3:24 I understand completely that no new memory needed to be allocated for the list when you modified it, but if you print out the memory address of a[0] before and after changing the value, both values use a different memory address. If the values inside the list require a memory address, doesn't that negated the performance improvements of using a list?
@@AlexTechie , that's what I thought about, too. I guess the performance gain comes from the fact that less data is being copied each time you make and assignment. If you store the result of the operation in one string, the whole result is being copied each time, which has a snowballing effect and slowing down every next operation, which is not the case if you mutate list items instead.
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.
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?
altho i dont think it matter's too much as garbage collection picks it up??? import gc a = 'hello' a += 'psds' a += 'kok' a += 'no way' refs = gc.get_referrers(a) print(len(refs)) #1 1 reference to `a` object,
@@anon-sl4vp Relying on GC to clean up poor memory management is not a good practice. This is one of the reasons against using Java. New object allocation doesn't just waste memory, it takes a lot of unnecessary time to do so, especially if it's on the heap.
@@Christopher-sl7cm Yeah GC can takes lots of time especially if there are lots of object on the heap, and should use it often like that. But it's not a bad practice in some scenario especially in a game industry where we run Garbage Collector on the loading time, for example, when changing to a new level. Also Garbage Collector only deallocates unused heap memory anyways unlike stack which get deallocated as soon as the variable it refers to goes out of scope.
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?
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...
I think Corey is saying that the process you mentioned slows down the speed of the program, since creating a new memory address and garbage collecting the old one is also a work for the CPU. And when that is performed millions amounts of times in a loop, it can severely deprecate the program speed.
The problem is with the copy operation. Imagine each iteration it copies 10 bytes, the in the 1000th iteration, it would be copying 1010 bytes... if u use a mutable list, then u will consistently copying 10 bytes into the list...
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.
Hi Corey, thanks for your video. If strings are immutable, how is it possible that you can append a new string to the original one? This is in reference to the last example in your video.
This is because it's actually creating a new object in the background. So when you append a new string to the original, it creates a new object with the string appended to the original. If strings were mutable, like lists, then it wouldn't need to create a new object to do this.
I don't get it totally. So when I make a variable like x="arbitrary string", then change to x="another string" , then the first one still remains in the memory or the memory space disengage?
My question exactly. I thought that the memory will be freed up, as the variable keeps being overwritten? So it's the constant overwriting that makes the program inefficient, not the accumulating amount of memory being claimed?
There is string buffer in Java too I think but also StringBuilder I always use. Is it that much of an issue? Is there no garbage collection in Python ? Good to see you have a series of technical terms to clarify.
If you did "a=[6,2,3,4,5]" the address would change, the same way a string would. I believe strings being placed in a different address is not a characteristic of their immutability. Please clarify.
The point is that you can change a single value of a mutable data type and it won't change its memory address, but you can't change a single value of an immutable type. It will throw an error.
Your last minute lines just cleared all things. But, how can I determine whether the method I am creating or functions I am using is mutable or immutable?
Thanks for this great explanation. I have a question. Can perfomance issues for immutable objects be solved by using recursions instead of loop iterations? I ask because functional languages use immutable data structures and a lot of those languages do not have loops originally in the language.
In the last section, you have used a list containing multiple strings. You showed that the various strings are stored at different addresses. As List is mutable, so is it like if I change the value in one of the string in the list it will modify it at the same address ?
The list would be modified and keep the same address. However, the address of the value in the list you changed would have a different address. For example: >>> l = ['a', 'b', 'c'] >>> id(l) 4387756040 >>> id(l[0]) 4386013736 >>> l[0] = 'd' >>> id(l) 4387756040 >>> id(l[0]) 4385877720 I hope that makes sense
Why not make strings mutable? What can I use in C# to make the string mutable? What about char[], is this mutable? Any consequences of not using string but using char[]?
Hi Corey, how is using remove an element in a list vs a string that could make more sense in immutable with mutable ? since, it is possible to replace a letter "o" or "a" or "A" in string but it's ID still point to the same memory location. >>> str2 = " hello " >>> str2.replace("o","a") ' hella ' >>> print id(str2) 41547264 >>> str2.replace("o","A") ' hellA ' >>> print id(str2) 41547264 >>> str2.replace(" ","A") 'AhelloA' >>> print id(str2) 41547264 #--- list--# >>> lst [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 'a', 2] >>> print id(lst) 41465776 >>> lst.pop(2) 4 >>> print id(lst) 41465776 >>>
Hey there. string.replace actually returns a new string and does not modify the original str2 variable you have set. So if you were to print str2 after doing the replace, it would still be equal to the original 'hello' string, hence why it's the same id. If you want to capture the replaces string then you need to set a new variable. Something like: replaced_str2 = str2.replace('o', 'a')
Thanks Corey.. i got it... >>> print id(str1) 61866752 >>> print "str1: {} and id {}". format(str1,id(str1)) str1: hello and id 61866752 >>> re_str1 = str1.replace("o","y") >>> print "re_str1: {} and id {}". format(re_str1,id(re_str1)) re_str1: helly and id 50813888 >>> print "str1: {} and id {}". format(str1,id(str1)) str1: hello and id 61866752
Hi there, great question. There are a lot of pros and cons to both mutable and immutable objects. I read a great answer to this question on one of the stackexchange sites, so I believe instead of copying their answer word-for-word, I'll just provide the link. This should answer some of your questions: programmers.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objects
String in java (like many other classes) gives an immutable object, this may have some performance issue but the advantage of this kind of object is that they are : ThreadSafe They have fast retrieving from memory, easiest for values copy
Why do you always use the word "whenever" when the word "when" would work just fine, and even be more appropriate in about half your cases? This is one of those mindless things people from the South do just to sound Southern, huh?