Mutable vs Immutable - Python

preview_player
Показать описание
In this video I go over a very detailed explanation of mutable and immutable objects in python. This is an extremely important concept and is is vital that you understand it if you program with python. Mutable objects are changeable and act differently in memory than other objects. You can create copies and aliases of them. Immutable objects are standard data types that cannot be changed or aliased.

Mutable Objects:
- dict
- list
- set
Immutable Objects:
- int
- str
- float
- bool
etc..

Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud

Please leave a LIKE and SUBSCRIBE for more content!

Tags:
- Tech
- Tech With Tim
- Python Tutorials
- Mutable Objects
- Immutable objects
- Mutable vs Imuttable python
- Python mutable
- Python immutable
Рекомендации по теме
Комментарии
Автор

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

kriz
Автор

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

terra_creeper
Автор

Man you have a gift. Nobody explains this stuff as clearly and logically as you do.

beckybonny
Автор

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 ♥

elkady-prodesiner
Автор

totally unknown to me. Thanks and greetings from Spain

eliasnoecollmartin
Автор

Clearly one of the best python youtubers ever

anonymouswanted
Автор

9:45 x and y will be pointing to the same string (at same location).

chinz
Автор

Thanks for this super important concept. Love it.

yuan
Автор

so good bro I finished learning python and didn't even know about this concept at all LOL tysm <3

karimalaa
Автор

Very well explained brother, keep up the good work

vishwasmagotra
Автор

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)

tunamusic
Автор

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()?

Nick_Reinhardt
Автор

0:00 intro
3:30 alias and cloning
6:40 memory model

Start from 9:25

sherlock_
Автор

Very helpful. Any other video of yours that I should watch for more tips/important concept? I'm new to Python. Thanks! :)

rinqiu
Автор

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.

gibbogle
Автор

16:42 ==> x=1000 and y=1000 have same id value. not different.

biraj
Автор

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

mikijasar
Автор

you should get a "huioin" graphic tablet to upgrade ur writing!

lebdesmath
Автор

pass by value = immutable and pass by reference = mutable... done. simple

tomjones
Автор

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.

williamwild
welcome to shbcf.ru