2

I know Python interns certain strings and creates a hash if the string starts with a letter or an underscore and only contains letters, underscores, or numbers as seen in Martijn Pieters codementor interview.

When assigning individually s = "$foo" and s1 = "$foo" s is s1 returns False as expected but using s, s1 = "$foo", "$foo" s is s1 returns True.

Why does python behave differently using the different assignments?

In [1]: s, s1 = "$foo", "$foo"    
In [2]: s is s1
Out[2]: True   
In [3]: s1 = "$foo"    
In [4]: s = "$foo"    
In [5]: s is s1
Out[5]: False
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 2
    I think you ought to look at that one: http://stackoverflow.com/questions/15541404/python-string-interning. it's quite clever what the interpreter can do – user3012759 Sep 15 '14 at 13:35
  • @user3012759, I already have there is nothing there about multiple assignments. – Padraic Cunningham Sep 15 '14 at 13:37
  • come to think of it, it's odd that you get s is s1 as false, do you get same results when you put it in one file and just python the file? – user3012759 Sep 15 '14 at 13:42
  • @user3012759, False is the correct output as the string starts with a dollar sign, I get the same results starting a new session for each assignment so there is definitely something in how python handles multiple assignments. – Padraic Cunningham Sep 15 '14 at 13:44
  • Just taking a guess here, but could it be that when you do `s, s1 ="$foo", "$foo"` it creates the objects with the same object id? – Renier Sep 15 '14 at 13:48
  • @Renier, yes that is what is happening but why it is doing it is the part I don't get. – Padraic Cunningham Sep 15 '14 at 13:51
  • @PadraicCunningham yep, interning rules escaped me... think we've got an answer now anyway – user3012759 Sep 15 '14 at 13:51

1 Answers1

2

As Martijn Pieters puts it, in the article you mentioned:

if a string starts with a letter or an underscore and only contains letters, underscores, or numbers, Python will intern the string

Your particular string contains $, so it will not be interned - what's happening here is not really related to string interning.

What's happening is that the interpreter is seeing you creating two references to equal immutable objects. Since the object types are immutable, a trivial optimization is not to create two objects, but simply choosing to reuse a single one.

To test our theory:

In [41]: a=1000

In [42]: b=1000

In [43]: a is b
Out[43]: False

In [44]: a,b=1000,1000

In [45]: a is b
Out[45]: True

Notice that this only happens because the interactive interpreter has to compile (and evaluate) each line you enter separately. If you put these statements into a script and execute it, the results are entirely different, because the compiler works on the whole code, and notices it:

a=1000
b=1000

print a is b

a,b=1000,1000

print a is b

output:

True
True
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • 2
    yes, the object is definitely being reused but how is python doing this internally? – Padraic Cunningham Sep 15 '14 at 13:54
  • @PadraicCunningham If you're asking *how the CPython compiler figures out that the objects are equal*, I'm afraid that exceeds my knowledge. Maybe someone familiar with the compiler will chime in – loopbackbee Sep 15 '14 at 14:13