What is a Variable in Python? Mutable vs Immutable

What is a Variable in Python? Mutable vs Immutable

Joseph Loves Python

7 месяцев назад

2,701 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Andy
Andy - 13.10.2023 01:54

come from sub. Fond of your clear voice and explanation! Excellent! Waiting more videos!

Ответить
Jürgen Erhard
Jürgen Erhard - 12.10.2023 15:41

"How variables are copied". They aren't, objects are.

"How variables are passed to functions". They aren't, objects are.

Variable is a bad term. Binding is a good term.

Ответить
Charbel Abi Daher
Charbel Abi Daher - 09.10.2023 02:33

Pretty cool

Ответить
Dhaval Ahir
Dhaval Ahir - 07.10.2023 13:16

# Python Memory Management #

1) If you create 1000 lists, even with the same values inside them, they will each occupy 1000 new memory locations. Python can't take the risk with mutable objects. If it creates the same memory for all 1000 variables, and if I change/update/append variable number 566 or any number of variables, at that time, all 1000 variables that are referring to only one single memory object will be affected.

Example :
A1 = ["Apple", "Banana"] # List mutable
A2 = ["Apple", "Banana"]
A3 = ["Apple", "Banana"]
...
...
...
A1000 = ["Apple", "Banana"]

--> all A1 to A1000 variable's have different memory locations even the values are same inside them ( which is doesn't matter).

2) If you create the same immutable object 1000 times with different variable names, all 1000 variables will refer to only one memory location. Because Python knows you can't change any of the variables out of the 1000 variables, hence the object is very safe in the memory location. That's why it needs only 1 memory location that is referred to by 1000 or 1 Crore variables.

Example :
A1 = "I am Immutable" # String immutable
A2 = "I am Immutable"
A3 = "I am Immutable"
...
...
...
A1000 = "I am Immutable"

--> all A1 to A1000 variable's have same memory locations because Python knows you can't change them anytime soon.

NOTE :

A = ("Apple", 45, ["Kiwi", "Banana"])

*This variable `A` is a tuple (immutable), but when you try to do

B = ("Apple", 45, ["Kiwi", "Banana"])
C = ("Apple", 45, ["Kiwi", "Banana"])
D = ("Apple", 45, ["Kiwi", "Banana"])
...
...
...
Z = ("Apple", 45, ["Kiwi", "Banana"])

Now, if you print the memory location of all the variables, they are all in different locations, and new memory locations are created 26 times (A to Z). But I had said all same immutable objects create only a single memory location, even if you define 1000 variables. That means I am wrong? No, please check again. The object ("Apple", 45, ["Kiwi", "Banana"]) is not immutable; it is mutable. You can use indexing to get the [2] list and perform append/extend/pop operations. That's why a new memory location is created each time because Python can't take risks with mutable objects.

Suppose in this case all 26 alphabets refer to only a single memory location, and imagine if I do
Y[2].append("Extra")
or
X[2].append("New String")
All 26 variables are affected in the memory location. This is scary. Python is so clever.

Ответить
chadi sfeir
chadi sfeir - 04.10.2023 18:11

Wow, this tutorial is absolutely fantastic! 🙌 Your explanation is clear and concise, and I love the way you break down complex topics. You have a real talent for teaching, and it's so easy to follow along with your explanations. Keep up the amazing work, and I can't wait to see more tutorials from you in the future!

Ответить
Joanna Moussa
Joanna Moussa - 01.10.2023 17:19

Awesome video !! Thanks for the clear explanation👌🏻

Ответить
lazy
lazy - 01.10.2023 17:18

Thanks for sharing this great content!

Ответить