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
7
  • 2
    I think you ought to look at that one: stackoverflow.com/questions/15541404/python-string-interning. it's quite clever what the interpreter can do Commented Sep 15, 2014 at 13:35
  • @user3012759, I already have there is nothing there about multiple assignments. Commented Sep 15, 2014 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? Commented Sep 15, 2014 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. Commented Sep 15, 2014 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? Commented Sep 15, 2014 at 13:48

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

yes, the object is definitely being reused but how is python doing this internally?
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.