How Python determines if two strings are identical

I tried to understand when Python strings are identical (they also use the same memory location). However, during my tests, there seems to be no obvious explanation when two string variables that are equal use the same memory:

import sys
print(sys.version) # 3.4.3

# Example 1
s1 = "Hello"
s2 = "Hello"
print(id(s1) == id(s2)) # True

# Example 2
s1 = "Hello" * 3
s2 = "Hello" * 3
print(id(s1) == id(s2)) # True

# Example 3
i = 3
s1 = "Hello" * i
s2 = "Hello" * i
print(id(s1) == id(s2)) # False

# Example 4
s1 = "HelloHelloHelloHelloHello"
s2 = "HelloHelloHelloHelloHello"
print(id(s1) == id(s2)) # True

# Example 5
s1 = "Hello" * 5
s2 = "Hello" * 5
print(id(s1) == id(s2)) # False

Strings are immutable, and as far as I know, Python is trying to reuse existing immutable objects, bearing in mind that other variables point to them instead of creating new objects in memory with the same value.

With that in mind, it seems obvious that Example 1returns True.
It is still obvious (to me) that it Example 2returns True.

It does not seem obvious to me that it Example 3returns False- I do not do the same as in Example 2?!?

SO:
Python "==" "is" ?

http://guilload.com/python-string-interning/ ( , , ), yougt - , , "" , HelloHelloHelloHelloHello Example 4. True.

, , , Example 2, ( , Example 4) - False?!?

, Python , .

- , ?

+13
2

, :

.pyc

, 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa' True? .pyc, ? , - Python . , - - ['foo!'] * 10**9? .pyc ! , , , , 20.

"HelloHelloHelloHelloHello", Python ( , , ). , , , , "Hello" * 5, Python " ", , . len("Hello" * 5) > 20, , .

EDIT:

, peephole.c, fold_binops_on_constants, :

// ...
} else if (size > 20) {
    Py_DECREF(newconst);
    return -1;
}

2:

, AST, ast_opt.c, fold_binop, safe_multiply, , MAX_STR_SIZE, 4096. , , .

+8

2:

# Example 2
s1 = "Hello" * 3
s2 = "Hello" * 3
print(id(s1) == id(s2)) # True

s1 s2 . true.

3:

# Example 3
i = 3
s1 = "Hello" * i
s2 = "Hello" * i
print(id(s1) == id(s2)) # False

s1 s2 , , false. , , HelloHelloHello .

intern, True

i = 3
s1 = "Hello" * i
s2 = "Hello" * i
print(id(intern(s1)) == id(intern(s2))) # True
+1

All Articles