So the is operator returns true for those. It works for some small strings because CPython, the reference implementation of Python, stores the contents separately, making all those objects reference to the same string content. If you construct two objects with the same contents, then it is usually not the case that the object identity yields true. To expand on Ignacio’s answer a bit: The is operator is the identity operator. Others might get entirely different results. It's also entirely possible that this is version dependent: I'm running python 2.7.3 on a macbook. So it seems that cPython keeps consistent memory addresses for strings containing only a-zA-Z when those strings are assigned to a variable. > id("bar")Ĭhecking the id of "bar" again after setting a equal to "bar" returns the same id. However, the id of "bar" and the id of a are different when checking the id of "bar" prior to setting a equal to "bar". a has been set to "foo" prior to checking the id. Here, the id of "foo" and the id of a are the same. However, this seems to only be the case when the string has been assigned to a variable: It seems that CPython has consistent memory addresses for strings containing only characters a-z and A-Z. Specifically, id returns the object's memory address. The is operator relies on the id function, which is guaranteed to be unique among simultaneously existing objects. Single characters always share memory, of course: > chr(0x20) is ' ' > 'aaaaaaaaaaaaaaaaaaaaa' is 'aaaaaaaa' + 'aaaaaaaaaaaaa' Non-alphanumeric string literals share memory if and only if they share the enclosing syntactic block: > y='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' Two string literals will share memory if they are either alphanumeric or reside on the same block (file, function, class or single interpreter command)Īn expression that evaluates to a string will result in an object that is identical to the one created using a string literal, if and only if it is created using constants and binary/unary operators, and the resulting string is shorter than 21 characters.Īlphanumeric string literals always share memory: > x='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' Well, at least for cpython3.4/2.7.3, the answer is "no, it is not the whitespace". Warning: this answer is about the implementation details of a specific python interpreter.
0 Comments
Leave a Reply. |