+ 5

Please help me understand this output. I don't get how Python handles Strings

I expected a == b and not a is b. But if a is b then I expected a is c+d. At least a is e. Did I miss a lesson? https://code.sololearn.com/c1nRq9S5pMOb/?ref=app

11th May 2021, 8:11 AM
Oma Falk
Oma Falk - avatar
21 odpowiedzi
+ 6
That's what namespacing concept is all about.. To avoid memory wastage, python provides namespace. Since a and b has same values, they refer to same memory. It was designed in such a way, For ex:- I have a variable called 'a' which consists of value 10 which occupies some space in some random free memory location "200"(assume) . Now, I defined another variable 'b' with same value 10. Now, Whenever python sees that stmt, it feels like the value 10 was already stored somewhere. So, no need to store it again, just refer that b to same memory location "200" (where 10 was previously stored). Same concept here.. Hope you got it this time..!!
11th May 2021, 9:08 AM
sarada lakshmi
sarada lakshmi - avatar
+ 3
c and d are not tuples. You need to write: ([stuff],)
11th May 2021, 10:46 AM
HonFu
HonFu - avatar
11th May 2021, 9:15 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
Frogged quote from you: " .... but why do a and b have same id? They are different variables." quote from linked code: "A variable in Python is always only a 'reference' to the actual value."
11th May 2021, 9:31 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
I think the specifics went somehow like this: For immutable objects, it doesn't matter if two similar objects are conflated to one. So a Python version is permitted to do it whichever way. One version may conflate two similar objects in one memory location while another doesn't, and even within one version, depending on the context, conflation may happen or not. This is something you aren't, as a user, supposed to care about. ;-) For mutable objects however, a Python version has to guarantee that conflation never ever happens.
11th May 2021, 9:40 AM
HonFu
HonFu - avatar
+ 2
Hehe, I understand, we always want to know. :) It might be one of these cases where you'd have to look into the source code. In ALL the versions's source codes. If you do all these tests with mutable objects, you should always get exactly the result you expect.
11th May 2021, 9:54 AM
HonFu
HonFu - avatar
+ 1
First, you should be very clear abt == and "is" operators and the concept called "Namespacing" . "==" compares the values irrespective of the memory they're located in, where as "is" operator results in True only when they're referring to same memory space. Now, namespacing is something where if two variables are having same values,then they refer to same memory. It is one of the beautiful feature that is provided by python (avoids memory wastage). Apply these concepts to the code written by you. Since, variable 'a' and 'b' are having same values, they refer to same memory where as the memory location of 'c','d' and 'c+d' are totally different. a = "Hello World" b = "Hello World" c = "Hello " d = "World" print(a == b,a is b,a== c+d,a is c+d) print(id(c+d)," ",id(a)," ",id(b)) #140136967668016 140136967556272 140136967556272 You can figure it out yourselves by adding this line.
11th May 2021, 8:32 AM
sarada lakshmi
sarada lakshmi - avatar
+ 1
sarada lakshmi In short is checks identity of an object. Just like is equals methods in Java?
11th May 2021, 8:35 AM
Atul [Inactive]
+ 1
Mihai Apostol HonFu does not cover this part. sarada lakshmi s explanation about namespacing although missing the very last step that lets me understand completely.
11th May 2021, 9:21 AM
Oma Falk
Oma Falk - avatar
+ 1
HonFu but I care about. 😇😇😇 I understood Python like this: 🤔🤔🤔🤔 Ohhh b is immutable lets see if this String is already in memory....ahhh yes. I won't waste memory space. 🤔🤔🤔🤔 Now e. Oh....it is an expression.... I must evaluate before I can decide...🤪nope I won't waste memory but also not time.
11th May 2021, 9:50 AM
Oma Falk
Oma Falk - avatar
+ 1
ok .... got it. For all those who discusses with me and might be interested: Python does not allocate new memory space at variable declaration, if an immutable object of same value is already in memory space. The right side must not be an expression but a value. Like so often: It is a behaviour not of all immutable objects but hashable objects. You can see in the last example, where tuples are immutable but not hashable.
11th May 2021, 10:39 AM
Oma Falk
Oma Falk - avatar
+ 1
HonFu yep fixed. But result is the same
11th May 2021, 10:50 AM
Oma Falk
Oma Falk - avatar
0
As you are concatenating c and d and comparing it with a it is not valid because they aren't referred to same memory #I am not a python programmer so after reading the article I have figured out this answer. Sorry if it is wrong
11th May 2021, 8:20 AM
Atul [Inactive]
0
Atul it increases my confusion. a and b are different objects. But a is b.
11th May 2021, 8:20 AM
Oma Falk
Oma Falk - avatar
0
The reason why this is the output is because, as you see the first three are true. a is b as “Hello World” is “Hello World”. However it is false for a is c+d as you have formed something new, which cant be identical as it is formed from two variables. Hope this helps
11th May 2021, 8:25 AM
Kamil Hamid
Kamil Hamid - avatar
0
https://youtu.be/mO_dS3rXDIs #Frogged I hope this will definitely help you
11th May 2021, 8:32 AM
Atul [Inactive]
0
sarada lakshmi sure .... but why do a and b have same id? They are different variables. and why than not e...(I added it)
11th May 2021, 9:02 AM
Oma Falk
Oma Falk - avatar
0
sarada lakshmi almost..... After your explanation I would expect id(e)==id(a). It also consists Hello World
11th May 2021, 9:13 AM
Oma Falk
Oma Falk - avatar
0
Frogged Like the way you understand Python
11th May 2021, 9:55 AM
Atul [Inactive]