Тёмный

Mutable vs Immutable - Python 

Tech With Tim
Подписаться 1,6 млн
Просмотров 21 тыс.
50% 1

Опубликовано:

 

8 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 45   
@kriz1718
@kriz1718 5 лет назад
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
@user-if9my3ee3w
@user-if9my3ee3w 5 лет назад
You're right. It can also be checked by this way: >>> x = 'str' >>> y = x >>> id(y) == id(x) True
@nlke182
@nlke182 4 года назад
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.
@williamwild8812
@williamwild8812 11 месяцев назад
This guys gets a lot wrong. So misleading to new programmers
@terra_creeper
@terra_creeper 2 года назад
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
@Hamheadon
@Hamheadon 2 года назад
Is there a deeper reason as to why the CPU creates copies for some data types and doesn't for the rest? Or is that just how it is?
@terra_creeper
@terra_creeper 2 года назад
@@Hamheadon it doesnt have anything to do with the cpu, its just how python implemented these data types
@GULLAPUDILIKITHBCE
@GULLAPUDILIKITHBCE 2 года назад
thanks best explanation
@GULLAPUDILIKITHBCE
@GULLAPUDILIKITHBCE 2 года назад
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
@beckybonny7552
@beckybonny7552 Год назад
Man you have a gift. Nobody explains this stuff as clearly and logically as you do.
@chinz3614
@chinz3614 2 года назад
9:45 x and y will be pointing to the same string (at same location).
@ahmedramzy892
@ahmedramzy892 7 месяцев назад
yes it have a same address of same obj
@eliasnoecollmartin3681
@eliasnoecollmartin3681 5 лет назад
totally unknown to me. Thanks and greetings from Spain
@elkady-prodesiner4172
@elkady-prodesiner4172 3 года назад
best teacher ever i watched most of your tutorials and i will watch it all, till i be like you thanks for being here for us ♥
@anonymouswanted3686
@anonymouswanted3686 2 года назад
Clearly one of the best python youtubers ever
@yuan6780
@yuan6780 4 года назад
Thanks for this super important concept. Love it.
@karimalaa204
@karimalaa204 4 года назад
so good bro I finished learning python and didn't even know about this concept at all LOL tysm
@gibbogle
@gibbogle 2 года назад
It looks as if equating a mutable y to x, for example: x = y, is like using a pointer in Fortran: x => y, where x is a pointer.
@vishwasmagotra5266
@vishwasmagotra5266 4 года назад
Very well explained brother, keep up the good work
@tunamusic2314
@tunamusic2314 3 года назад
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)
@terra_creeper
@terra_creeper 2 года назад
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
@s.muneeba3944
@s.muneeba3944 2 года назад
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
@krypton9378
@krypton9378 3 года назад
Respect from Bangladesh!
@niloydey6147
@niloydey6147 3 года назад
Very helpful. Thanks for uploading.
@rinqiu5300
@rinqiu5300 3 года назад
Very helpful. Any other video of yours that I should watch for more tips/important concept? I'm new to Python. Thanks! :)
@Nick_Reinhardt
@Nick_Reinhardt 11 дней назад
Why did it print when you typed: >>> x + 's' ? I get why the print was 'strings', but why did it print at all when you didn't type print()?
@gopalsv5230
@gopalsv5230 3 года назад
cool and simple ... thnks
@mjj2u2
@mjj2u2 2 года назад
Well done.
@joeljose182
@joeljose182 4 года назад
Wow you saved my life thanks
@kasyapdharanikota8570
@kasyapdharanikota8570 3 года назад
very well explained
@Zaphodikus
@Zaphodikus 5 лет назад
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.
@mikijasar2594
@mikijasar2594 6 месяцев назад
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
@sherlock_221
@sherlock_221 2 года назад
0:00 intro 3:30 alias and cloning 6:40 memory model Start from 9:25
@snape0001
@snape0001 2 года назад
17:53
@kartiksharma6576
@kartiksharma6576 3 года назад
Grt
@lebdesmath2510
@lebdesmath2510 2 года назад
you should get a "huioin" graphic tablet to upgrade ur writing!
@tomjones8293
@tomjones8293 Год назад
pass by value = immutable and pass by reference = mutable... done. simple
@biraj4893
@biraj4893 2 года назад
16:42 ==> x=1000 and y=1000 have same id value. not different.
@thepoorsultan5112
@thepoorsultan5112 2 года назад
He lied
@user-rq9kd7xu5x
@user-rq9kd7xu5x 3 года назад
You are amazing 🤩
@blmppes9876
@blmppes9876 5 лет назад
Love you!
@victorlefeu
@victorlefeu 3 года назад
thanks!
@rollsamantha2673
@rollsamantha2673 3 года назад
my man
@fumano2679
@fumano2679 Год назад
Fortnite on Desktop thats my man!
@williamwild8812
@williamwild8812 11 месяцев назад
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.
Далее
Python 101: Learn the 5 Must-Know Concepts
20:00
Просмотров 1,1 млн
Dropping In from the Clouds 🌁
00:17
Просмотров 848 тыс.
Самое неинтересное видео
00:32
Просмотров 1,2 млн
5. Tuples, Lists, Aliasing, Mutability, and Cloning
41:28
Immutable vs Mutable Objects in Python
9:55
Просмотров 63 тыс.
Modern Python logging
21:32
Просмотров 183 тыс.
25 nooby Python habits you need to ditch
9:12
Просмотров 1,7 млн
Immutable and mutable in python
17:10
Просмотров 34 тыс.
What is a Variable in Python? Mutable vs Immutable
13:26
Python Generators Explained
28:37
Просмотров 154 тыс.
What is Pandas? Why and How to Use Pandas in Python
10:08