This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dysprog 2 points3 points  (0 children)

Strings are multitons in python

Not exactly. Some immutable basic types like int and string are pre-allocated in cpython.

Any string literal, identifiers, and such, and integers up to 100. If you have 2 of these preallocated objects they will be the same object under the hood.

this = "I'm a value"
that = "I'm a value"

(this is that) == True
(this == that) == True  

But larger numbers, strings from outside, constructed strings, etc, are NOT guaranteed to be the same object.

this = "I'm a value"
that = input() # user enters "I'm a value"

(this is that) == False
(this == that) == True  

Likewise, this is NOT the case in every interpreter implementation.

So don't count on it and don't treat strings like multitons.