Is Immutability A Lie In Python?

  Рет қаралды 20,563

Indently

Indently

Күн бұрын

Пікірлер: 60
@thodorisevangelakos
@thodorisevangelakos Жыл бұрын
Havent watched the video yet, but as far as the thumbnail is concerned there's no actual overriding of values being done. You just replace the entire object with a newly constructed one
@Indently
@Indently Жыл бұрын
For experienced programmers it should be rather obvious :)
@renren_does_programming
@renren_does_programming 9 ай бұрын
Its kinda wasteful I think, because it creates a new object instead of reusing the old one. But I guess that way its actually mutable. Python is weird.
@rje4242
@rje4242 Жыл бұрын
I think an important thing to point out is that "Immutable" doesn't mean CONST.. I can't count on the value of a tuple to not change once assigned.
@Indently
@Indently Жыл бұрын
But even const doesn’t mean const in Python
@rje4242
@rje4242 Жыл бұрын
@@Indently yes - not in Python. in Ruby and C++ it does. when people hop between languages it's easy to miss that.
@Bl0xxy
@Bl0xxy 8 ай бұрын
@@Indently there's no such thing as a const in python lol
@TrimutiusToo
@TrimutiusToo Жыл бұрын
Well me who has over a decade of experience as software developer was like "that is quite a stupid question about immutable types", but then I stop and think and do realize that amateurs and newbies get stumped by internal workings of such classes often
@stevenbrown9185
@stevenbrown9185 Жыл бұрын
So what SHOULD you do if you needed to do a lot of string manipulation? This seems to imply that well, DON'T, but i feel like I'm missing something.
@Aman-yk8zr
@Aman-yk8zr Жыл бұрын
I usually .split() into a list, petform the manipulations, then .join() back into a string
@thomaseb97
@thomaseb97 11 ай бұрын
if performance is a huge issue python is the wrong language, in all reality its difficult to make string manipulation in python efficient as it doesnt have a struct and chars you would still just use strings you can try a few methods and benchmark, normal + operation, .split and .join, f string or string.format if possible otherwise there might be libraries out there which offers string manipulation where its implemented in a performant language and has a python api
@ivandeor9159
@ivandeor9159 11 ай бұрын
When i had to do this i just created a list of characters and treated it like a mutable string. A bit less readable, but it should be faster
@ivandeor9159
@ivandeor9159 11 ай бұрын
​@@Aman-yk8zrthat could work too, but i'm pretty sure that every time you call .split method, it has to go through every element of the string in order to put it inside the list, which might add up if you're doing it many times
@mathieuflouret7177
@mathieuflouret7177 9 ай бұрын
Hey there ! Just use a StringIO from the io built-in library, especially if you need to "construct" a string from scratch. It works with write and get methods, so you can pretty much write to it and get it as an immutable string. Basically, it's the best of both worlds : you get a mutable string buffer when creating/constructing/processing it, and then keep the actual immutable string for runtime performance/memory efficiency.
@evlezzz
@evlezzz Жыл бұрын
What a wrong and messed up explanation.... The whole misunderstanding happend because of mixing up two entirely different things: objects and variables. Mutability is a trait of an object, not variable and whole idea of immutable **objects** is that their value cannot be changed. Saying that "we created a whole new variable" is simply wrong. We did create a new object as the result of an operation on some other objects, and than put this object (basically a link to it) into the same variable that we used before. At this stage there is no difference between mutable and immutable objects at all. Doing something like `lst = [1, 2, 3]; lst = lst + [4, 5]` will create a new list in variable `lst` as it would for tuples and strings despite list being mutable type. The only difference happens for operations like `+=`. Docummentation suggests that it should perform given operation in place (without creating new object) whereever possible and fallback to creating new object when operation itself make sense but object involved is immutable.
@rajarajankannan1516
@rajarajankannan1516 11 ай бұрын
see immutibility isnt a lie because , mutable means you can change thing in the element . like list or dictionary but in string or tuple you created a new elements with a change . My statement is that in list you dont need to create new list to make changes you want in it , but string or tuple requires it and at the end you can use replication operator and slicing operator in tuple or string also because you are printing it but that doesnt make any change in main tuple or string
@MrBoydgo
@MrBoydgo 2 ай бұрын
Please keep in mind that in all these cases, the data structure itself cannot be changed, but the variable can be reassigned to a new instance of the structure. This concept of immutability means that once you create these objects, their state remains constant throughout their lifetime, while the references (variables) can point to different instances. So, when we say that tuples are immutable, it specifically refers to the tuple object itself, not the variable that references it. The variable can be reassigned to point to a new tuple, but the contents of the original tuple cannot be changed. This distinction is important when discussing immutability in Python.
@barrowmusics
@barrowmusics 2 ай бұрын
There's difference between changing a string object and concatenating to a string, What you just did does not change the initial string if you print text variable
@M-F-H
@M-F-H 6 ай бұрын
The first example is particularly badly chosen, "text = text + '!' " will always create a new object, even if text is mutable! (For example, LIST = LIST + [0] does create a NEW list (if you do LOST = LIST = [] before this, then LOST will show that the old LIST = [] continues to exist after LIST = LIST + ... -- that is, this instruction will NEVER modify an object, even if mutable, but always create a new, distinct object). So you should *at least* have written " text += '!' " in order to illustrate what you want to illustrate!
@manu-c9o
@manu-c9o 11 ай бұрын
i love your ide theme , can you tell me howd u get it
@maswinkels
@maswinkels 8 ай бұрын
I'm still waiting, and have been for years, for a sensible answer to the question why immutability exists in Python, other than to irritate programmers and to make their lives harder. I'm convinced that their must be a good reason, but I just haven't discovered it yet.
@saeedgnu
@saeedgnu Жыл бұрын
Immutable or mutable values has nothing to do with how these values are accessed (through variables, or consts in some languages). Python has no consts. And variables are variables. Two different subjects.
@Indently
@Indently Жыл бұрын
I don’t understand what you’re arguing in relation to what I said in the video
@murphygreen8484
@murphygreen8484 Жыл бұрын
I think someone didn't watch the video
@ncmathsadist
@ncmathsadist 4 ай бұрын
Mutability is the property of an object. Variables are typeless names.
@ncmathsadist
@ncmathsadist 4 ай бұрын
You say, "You create a whole new variable." Incorrect: You create a whole new OBJECT on the heap. The stack stores variables and the memory addresses they point at. The heap stores the actual object.
@watchmakerful
@watchmakerful 8 ай бұрын
Wouldn't it be better to implement two different types for mutable and immutable strings, like they are implemented for sets (set and frozenset)?
@NathanSMS26
@NathanSMS26 Жыл бұрын
Do other languages not allow reassignment of immutable variables?
@Me21000
@Me21000 Жыл бұрын
thank you for this information sir, much love
@JeersNX
@JeersNX Жыл бұрын
can you use: str; string = "Hello"; if string[0] == "H": string.replace(string[0], "o");
@JeersNX
@JeersNX Жыл бұрын
or? makes sense
@Indently
@Indently Жыл бұрын
replace returns a copy of the original
@JeersNX
@JeersNX Жыл бұрын
@@Indently oh
@cn-ml
@cn-ml 4 ай бұрын
The data is immutable, not the variables
@MrBoydgo
@MrBoydgo 2 ай бұрын
You're right. The immutability refers to the contents of the data structure (like a tuple, string, or frozenset), which cannot be changed once created. However, the variable itself can be reassigned to reference a different instance of that data structure.
@Diegos79
@Diegos79 Жыл бұрын
Great video as always
@pokerchannel6991
@pokerchannel6991 3 ай бұрын
you can do that and yes, it is memory inefficient and resource inefficient, but I guess python make an easy way for you to make a new variable. But, what you cannot do is my_tuple[0] = 'new value' LOL. That is the immutability of it. But you already knew that, I am just clarifying for ppl new to py
@kumaranb8702
@kumaranb8702 7 ай бұрын
Very nice 👍🙂❤
@juanetehOK
@juanetehOK 6 ай бұрын
and how should be done in python then? i was waiting for the solution...but the video stops right there 😂
@davidm.bm01
@davidm.bm01 Жыл бұрын
PyTuple_SetItem
@rohithreddy75
@rohithreddy75 Ай бұрын
I thought its a faceless channel
@michaeldavid6832
@michaeldavid6832 3 ай бұрын
Python was named after Monty Python. Pronounce it like you would that one. "PY-thon" not "Pythin"
@MrBoydgo
@MrBoydgo 2 ай бұрын
Good observation! Maybe he is not a Pythonista yet! 😂❤
@michaeldavid6832
@michaeldavid6832 2 ай бұрын
@@MrBoydgo We all strive for perfection but none of us will achieve it. If you don't try, you won't get even close.
@MrBoydgo
@MrBoydgo 2 ай бұрын
@@michaeldavid6832 Agreed!
@firstname4337
@firstname4337 8 ай бұрын
text[0] = 'a' should absolutely work -- I know "this is just how python works", but it really is a poor design decision on Guido van Rossum's part -- but then I also think he should have used { } instead of ridiculous indentations
@kumaranb8702
@kumaranb8702 7 ай бұрын
Hi
@gardnmi
@gardnmi Жыл бұрын
If you are concerned about the performance cost of updating a tuple then you shouldn't be using python for your application.
@Indently
@Indently Жыл бұрын
I like getting the best performance out of Python for the least effort. Using the correct data type is a free performance boost that costs nothing.
@sg8nj
@sg8nj Жыл бұрын
Great😂
@JeersNX
@JeersNX Жыл бұрын
F i r s t
@JeersNX
@JeersNX Жыл бұрын
btw hello idently
@Indently
@Indently Жыл бұрын
Idently :')
@JeersNX
@JeersNX Жыл бұрын
This vid was really interesting@@Indently
@Ajmal_Yazdani
@Ajmal_Yazdani 3 ай бұрын
@Indently. Thanks for share. one question? why id of string are same?: BEFORE: 140736876352624 hello! AFTER: 140736876352624
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 431 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
Mobile Development in 2025 - Native or Cross Platform?
16:21
Stefan Mischook
Рет қаралды 11 М.
5 Python Libraries You Should Know in 2025!
22:30
Keith Galli
Рет қаралды 82 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 687 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Immutable vs Mutable Objects in Python
9:55
Real Python
Рет қаралды 65 М.
How I Would Learn Python FAST (if I could start over)
12:19
Thu Vu data analytics
Рет қаралды 688 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 166 М.
3 Bad Python Habits To Avoid
10:40
Indently
Рет қаралды 57 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 340 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН