0

Lets say a = 10000000000 and b = 10000000000 i.e. both a and b have the same value.

When I print id() of a and b it always remains same no matter how many times I run the code.

Also, it remains same for float, string, boolean and tuple but does not remain same for lists, sets and dictionaries.

Does that mean when multiple variables (immutable types) have the exact same value it always point to a single object in memory and hence a is b will always return True, whereas multiple variables of mutable type having the same value point to its unique object in memory and hence a is b will always return False?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Does this answer your question? [Two variables in Python have same id, but not lists or tuples](https://stackoverflow.com/questions/38189660/two-variables-in-python-have-same-id-but-not-lists-or-tuples) – mujjiga Oct 31 '20 at 15:17
  • Thanks for your comment mujjiga but to test this I took large values and ran it multiple times but always got the same answer in contrary to that question. – Sujit_Singh Oct 31 '20 at 15:21
  • Does this answer your question? [What's with the integer cache maintained by the interpreter?](https://stackoverflow.com/questions/15171695/whats-with-the-integer-cache-maintained-by-the-interpreter) – Tomerikoo Oct 31 '20 at 15:24
  • Thanks for your comment Tomerikoo. I am so sorry but I didn't quite understand that answer as I am kind of a beginner. I just want to know if my interpretation is correct or not. – Sujit_Singh Oct 31 '20 at 15:33
  • Yes your observation is correct but **you can't rely on it** , and it is exactly the reason [why you shouldn't use `is` with ints](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – Tomerikoo Oct 31 '20 at 15:48

1 Answers1

1
...it always point...

In general yes, but it is not guaranteed. It is a form of Python internal optimization known as type kerning.

You should look at it like something that does not matter for immutables, something transparent for the language user. If the object has a value that cannot change, it does not matter what instance of the objects of that type (and with that value) you are reading. That is why you can live with having only one.

As for the tuples, note that the contained objects can change, only the tuple cannot (that is, change the number of its elements).

So for immutables you do not have to worry.

For mutables, you should be careful, not with Python internal optimizations but with the code you write. Because you can have many names referring to the same instance (that now can be changed through any one of these references) and one change will be reflected in all of them. This is more tricky when passing mutables as arguments, because far away code can change the object (what was passed was a copy of the reference to the object, not a copy of the object itself).

It is your responsability to manage things with mutables. You can create new instances with the same values (copies) or share the objects. You can even pass copies as arguments to protect yourself from unintended side effects of calls.

progmatico
  • 4,714
  • 1
  • 16
  • 27
  • Thanks a lot progmatico so I can consider that my interpretation is not true and no matter what the object is (mutable or immutable) it may or may not point to the same object and I should not worry much about it. Am i right ? – Sujit_Singh Oct 31 '20 at 15:35
  • This answer is indeed very helpful progmatico – Sujit_Singh Oct 31 '20 at 16:11