One correction: at 7:58 you are saying that when we create a new string x="str" and assign y=x , y points to a new location but in reality y points to the same location as x. Only if we change y then will y point to a different location. See the code below. x='str' y=x print(x is y) y='newStr' print("x is", x, "and", "y is ", y) output: True x is str and y is newStr Please correct me if i am wrong
Yeah, this part was totally wrong in the video, I had to double check it myself since it didn't make sense in the video. Glad someone else caught this and posted the correctiong.
one important thing you didn't explain that well is that python treats variables differently than other languages for example in java, variables are containers for data whereas in python, variables are names pointing to objects in memory so when you assign a value to a variable, you actually just change which place in memory that variable is pointing to under this light, the difference between immutable and mutable types becomes clearer: mutable types are objects you can directly modify without changing the id of the object or creating a new one, so all variables pointing to it will continue pointing to that modified object immutable types are objects you cannot modify, you can only create new objects based on the object you wanted to change, so the old object and all variables pointing to it will remain unchanged so if you enter: x = "str" y = x you will have two variables pointing to the same string object in memory but if you enter: y = x + "s" you create a new object in memory and let y point to it but if you enter: x = [1,2,3] y = x y.append() you will still have two variables pointing to the same object, because the append() method directly modifies the list without creating a copy
what i understand is: if i copy a mutable object since it can change without changing ids therefore it will effect parent object also if i copy immutable object since it will change ids when it is changed therefore it wont effect parent object...... thanks even after this lecture i can't find out why this happens but after reading comment i got some clarity
i got one thing i don't understand, x = 'str' y = x you said it create two different objects, but why when i print(id(x)) and print(id(y)) they print the same, it means they have the same id ( address) and points the same value ( 'str' here)
When you assign a value to a variable you always change which object the variable points to, even with mutable types. so when you type: x = "str" you create a new string object and let 'x' point to it and by typing y = x you let y point to the same object The actual difference between mutable and immutable types is that with mutable types, you can modify the actual object, but with immutable types you can only ever create new objects based on the first
yeah theres a bit of a mistake in the video above. you can think of immutable objects creating a "temporary" alias meaning they have the same id as long as u dont change it but as soon as u change either x or y then they both become completely different objects with different locations. and fun fact: if you decide to change them back like for eg you had x=5 and y=5 (same ids) and then you changed y x=5 and y=7 (diff ids) NOW if you changed x to be equal to 7 then both would have the same ids again
A slightly longer way of explaining this, is brought to you in a beautiful blog post I found while googling a good answer, which is sadly nowhere to be found. The second best I got was nedbatchelder.com/text/names.html I found, but your clip explains it so much more accessibly.
Thanks for your video. I am a beginner in learning programming, but I have a small problem with understanding this video of yours. In your video at 8.15 min, you say that x is one string 'str' and y points to another 'str'. So according to you, they are two different objects. Then how do you explain that their id() is the same?? so it's the same object ?? Please reply and thanks in advance x = 'str' y = x print(id(x), x, 'x') print(id(y), y, 'y') x += '5' print(id(x), x, 'x') print(id(y), y, 'y') ------------------------------------ 2847580343728 str x 2847580343728 str y 2847588873840 str5 x 2847580343728 str y
At 9:10 you are wrong especially by the definition of immutable you CANT change 'str' to 'str5'. In idle: x = str x 'str' id(x) 4349951664 x+=5 x 'str5' id(x) 4384082480 The ids show it is a new memory location. You cant change 'str' . Its freaking immutable. Watch this video with a grain of salt and test things yourself.