+ 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
21 Respuestas
+ 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..!!
+ 3
c and d are not tuples.
You need to write:
([stuff],)
+ 2
+ 2
+ 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.
+ 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.
+ 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.
+ 1
sarada lakshmi In short is checks identity of an object. Just like is equals methods in Java?
+ 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.
+ 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.
+ 1
HonFu yep fixed. But result is the same
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
0
Atul it increases my confusion.
a and b are different objects.
But a is b.
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
0
https://youtu.be/mO_dS3rXDIs
#Frogged I hope this will definitely help you
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)
0
sarada lakshmi almost.....
After your explanation I would expect id(e)==id(a).
It also consists Hello World