7

I'm aware that in python every identifier or variable name is a reference to the actual object.

a = "hello"
b = "hello"

When I compare the two strings

a == b

the output is

True

If I write an equivalent code in Java,the output would be false because the comparison is between references(which are different) but not the actual objects.

So what i see here is that the references(variable names) are replaced by actual objects by the interpreter at run time.

So,is is safe for me to assume that "Every time the interpreter sees an already assigned variable name,it replaces it with the object it is referring to" ? I googled it but couldn't find any appropriate answer I was looking for.

jamylak
  • 128,818
  • 30
  • 231
  • 230
tez
  • 4,990
  • 12
  • 47
  • 67
  • 2
    For the *"Every time the interpreter sees an already assigned variable name, it replaces it with the object it is referring to"*. Actually, each existing Python variable was assigned a reference. Think about the reference as about the address of the real object. In every language, any usage of a variable (except of assignment) means by definition the same as usage of its content. The content is the reference value. It is usual in many languages that the reference values are automatically dereferenced, and then it looks as if you were directly working with the target object. – pepr Aug 07 '12 at 06:04

3 Answers3

17

If you actually ran that in Java, I think you'd find it probably prints out true because of string interning, but that's somewhat irrelevant.

I'm not sure what you mean by "replaces it with the object it is referring to". What actually happens is that when you write a == b, Python calls a.__eq__(b), which is just like any other method call on a with b as an argument.

If you want an equivalent to Java-like ==, use the is operator: a is b. That compares whether the name a refers to the same object as b, regardless of whether they compare as equal.

Danica
  • 28,423
  • 6
  • 90
  • 122
  • 3
    except: `a = "test"; b = "test"; a is b == True` – Hamish Aug 07 '12 at 02:49
  • 5
    @Hamish Again, because of string interning. If you get the string as input, e.g. `a = raw_input(); b = raw_input()` and type "test" both times, `a is b` will be `False`. – Danica Aug 07 '12 at 02:50
  • Yeah, just needs to be said, for clarity. – Hamish Aug 07 '12 at 02:51
  • 1
    "regardless of whether they compare as equal" - it's worth noting (tangentially) that `a is b` *usually* implies `a == b` (although it is perfectly possible for something to compare unequal to itself). – lvc Aug 07 '12 at 03:09
9

Python interning:

>>> a = "hello"
>>> b = "hello"
>>> c = "world"
>>> id(a)
4299882336
>>> id(b)
4299882336
>>> id(c)
4299882384

Short strings tend to get interned automatically, explaining why a is b == True. See here for more.

Community
  • 1
  • 1
Noah
  • 21,451
  • 8
  • 63
  • 71
6

To show that equal strings don't always have the same id

>>> a = "hello"+" world"
>>> b = "hello world"
>>> c = a
>>> a == b
True
>>> a is b
False
>>> b is c
False
>>> a is c
True

also:

>>> str([]) == str("[]")
True
>>> str([]) is str("[]")
False
John La Rooy
  • 295,403
  • 53
  • 369
  • 502