5

In python, fuction id() returns the memory address of the object. (https://docs.python.org/2/library/functions.html#id)

and in python, if two variables have the same value, then do they have same address?



In [30]: a = 'asdf'

In [31]: hex(id(a))
Out[31]: '0x1082caa08'

In [32]: b = 'asdf'

In [33]: hex(id(b))
Out[33]: '0x1082caa08'

In [34]: c = 2

In [35]: hex(id(c))
Out[35]: '0x1067295c0'

In [36]: d = 2

In [37]: hex(id(d))
Out[37]: '0x1067295c0' 
deceze
  • 510,633
  • 85
  • 743
  • 889
Jason Kim
  • 143
  • 1
  • 9
  • 3
    string & integer interning is implementation defined. So yes and no. depends. – Jean-François Fabre Apr 26 '18 at 13:23
  • "if two variables have the same value, then do they have same address?" — Sometimes. – khelwood Apr 26 '18 at 13:24
  • 1
    I wouldn't worry for all this `is` and `id` stuff. Leave that to the python coders. – Jean-François Fabre Apr 26 '18 at 13:25
  • Just discussed this subject this afternoon, here: https://stackoverflow.com/questions/50037548/python-is-operator-behaviour-with-string/50037786?noredirect=1#comment87093207_50037786 – BcK Apr 26 '18 at 13:26
  • @Jean-FrançoisFabre I [do not think it is an exact duplicate](https://meta.stackoverflow.com/questions/292329/does-the-same-answer-imply-that-the-questions-should-be-closed-as-duplicate). It has the same answer, but the question is neither about `is` operator nor exclusively about strings. – abukaj Apr 26 '18 at 13:32
  • that's why I added several duplicate links. The question is a composite duplicate. – Jean-François Fabre Apr 26 '18 at 13:37
  • now if you have a better duplicate to suggest, there's still one slot left :) – Jean-François Fabre Apr 26 '18 at 13:42
  • (note that the question doesn't match the snippets. There's no `float` test or lists, or tuples, whatever, which makes the question too broad) – Jean-François Fabre Apr 26 '18 at 13:43

0 Answers0