[open]Question About Python objects | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

[open]Question About Python objects

a = 'i am good' b = 'i am good' print(a is b) # output is true a+='88' print(a) # output is i am good88 print(b) # output is i am good If both the variables refer to same object(as a is b is true) now if I change one , shouldn't the other variable too change? Why??( I know as per the code since different variables are used to store values, changing one doesn't change the other ,but in terms of object concept they are referring to same object) a = [1,2,3] b = [1,2,3] print(a is b) #output is False Why ?? I guess there is something with the mutability of an object.

1st Nov 2021, 5:17 AM
Prabhas Koya
19 Answers
+ 10
It is simply a decision of python. Two equal strings are the same object. Two equal lists are not. Lists tend to change while strings tend to be constant.
1st Nov 2021, 7:51 AM
Oma Falk
Oma Falk - avatar
+ 10
here is a nice tutorial done by HonFu about this topic: https://code.sololearn.com/cjN801sfz6PH/?ref=app
2nd Nov 2021, 6:53 AM
Lothar
Lothar - avatar
+ 6
My 2 cent: Strings are immutable. So 'a' and 'a' are the same object, because none will ever change. When you change the string assigned to a variable, you actually assign a *different* object to it. Lists are mutable. So you can change any list at any time, and it's still the same object. This way, two identical lists can become different at any time, while each one is always the same object. So, different lists *have* to be different objects, even if their contents are the same.
2nd Nov 2021, 3:21 AM
Emerson Prado
Emerson Prado - avatar
+ 5
FINALLY - IT MIGHT BE BECAUSE TO MAKE PYTHON EFFICIENT THANKS TO ALL Rik Wittkopp Oma Falk Python Learner I'm A Baked Potato
1st Nov 2021, 8:03 AM
Prabhas Koya
+ 5
Prabhas Koya Check out this one maybe it can help you.. https://code.sololearn.com/cbSHoOeX1Guj/?ref=app
1st Nov 2021, 8:33 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 5
Hi! You can use sys.getrefcount in the sys module to get the number of references to an object: >>> import sys >>> print(sys.getrefcount(5)) 37 >>> a = b = c = d = 5 >>> print(sys.getrefcount(5)) 41
1st Nov 2021, 9:51 PM
Per Bratthammar
Per Bratthammar - avatar
+ 4
Prabhas Koya I don't know the reason behind the action of Python when allocating memory. I would assume it has something to do with the underlying methods associated with the types being allocated. We are dealing with 2 different types that seem to have some commonality when it comes to slicing, etc.... But they differ in many other ways, so one must assume the underlying logic differs. Here is a little code that won't answer your questions, but may help you find your way. Great question by the way 😁👍 a = [1,2,3] b = [1,2,3] print(a is b) print(id(a)) print(id(b))
1st Nov 2021, 7:55 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Rik Wittkopp what about the lists ,they are referencing to different object unlike strings? why?
1st Nov 2021, 5:57 AM
Prabhas Koya
+ 3
Rik Wittkopp JUMP_LINK__&&__Python__&&__JUMP_LINK Learner My question is when string object instances are created they are referring the same object for different instances , unlike list object instances are creating different objects for different instances, and also if we make two variables refer same list object changing one variable changes the other but strings aren't the same ,they are pointing to different address the moment we changed one of them I guess there is something with mutability, so I want to know all about how the object is stored .
1st Nov 2021, 6:14 AM
Prabhas Koya
+ 3
a = 'prabhas' b= 'prabhas' print(a is b) # this is true a=[1,2,3] b=[1,2,3] print(a is b) # this is false # why can't both be true?
1st Nov 2021, 7:12 AM
Prabhas Koya
+ 3
Your Conclusion: *internal state of the immutable object can't be changed. *So python create a new object in the memory This is what I was thinking there might be some thing to do with mutability of an object *Mutable Object NEVER point to same address, even if two object is identical.(IS THIS TRUE FOR EVERY CASE???) THANKS
1st Nov 2021, 9:49 AM
Prabhas Koya
+ 3
Савелий Иванов Begin to start a course, but it’s better to ask your questions in new thread.
2nd Nov 2021, 4:02 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Prabhas Koya You have 2 variables with identical contents, so python has just created one address to save memory and assigned 2 pointers to that address. But when 1 variable changes the nature of the contents, 2 addresses are created. One for each variable. a = 'i am good' b = a print(a) print(b) print(a is b) print() a += "88" print(a) print(b) print(a is b)
1st Nov 2021, 5:49 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Hi Prabhas! I hope this question clears your both doubts in different answers. https://stackoverflow.com/questions/13650293/understanding-pythons-is-operator
1st Nov 2021, 5:56 AM
Python Learner
Python Learner - avatar
+ 2
This might be helpful for your latest question. https://stackoverflow.com/questions/15541404/JUMP_LINK__&&__python__&&__JUMP_LINK-string-interning My first link has everything what you want. I don't think I have to copy the main points and share them with you.
1st Nov 2021, 6:38 AM
Python Learner
Python Learner - avatar
+ 1
Oma falk i agree with you. And moreover, to answer the why isn’t b changing if i change the a sting: the compiler follows the order of the program. If you’d write b=a and then print(a is b) you’ll get True as an output
2nd Nov 2021, 6:45 AM
Veronica Spialtini
Veronica Spialtini - avatar
6th Nov 2021, 2:37 PM
Савелий Иванов
0
https://sololearn.com/discuss/2917748/?ref=app
6th Nov 2021, 2:38 PM
Савелий Иванов
- 2
a = [ 1, 2, 3 ] b = a if b is a: print("a and b are same") else: print("a and b are not same") na = "it is string literal" nb = "it is string literal" if nb is na: print("na and nb are same") else: print("na and nb are not same") ma = [ 1, 2, 3 ] mb = [ 1, 2, 3 ] if mb is ma: print("ma and mb are same") else: print("ma and mb are not same") print(id(a)) print(id(b)) # a is same as b print(id(na)) print(id(nb)) # na and nb identical print(id(ma)) print(id(mb)) # not identical Looks like it a identity reason DHANANJAY
2nd Nov 2021, 11:34 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar