I've started learning Python (python 3.3) and I was trying out the is
operator. I tried this:
>>> b = 'is it the space?'
>>> a = 'is it the space?'
>>> a is b
False
>>> c = 'isitthespace'
>>> d = 'isitthespace'
>>> c is d
True
>>> e = 'isitthespace?'
>>> f = 'isitthespace?'
>>> e is f
False
It seems like the space and the question mark make the is
behave differently. What's going on?
EDIT: I know I should be using ==
, I just wanted to know why is
behaves like this.
Warning: this answer is about the implementation details of a specific python interpreter. comparing strings with
is
==bad idea.Well, at least for cpython3.4/2.7.3, the answer is "no, it is not the whitespace". Not only the whitespace:
Two string literals will share memory if they are either alphanumeric or reside on the same block (file, function, class or single interpreter command)
An 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.
Single characters are unique.
Examples
Alphanumeric string literals always share memory:
Non-alphanumeric string literals share memory if and only if they share the enclosing syntactic block:
(interpreter)
(file)
Output:
True
andFalse
For simple binary operations, the compiler is doing very simple constant propagation (see peephole.c), but with strings it does so only if the resulting string is shorter than 21 charcters. If this is the case, the rules mentioned earlier are in force:
Single characters always share memory, of course:
In fact your code amounts to comparing objects id (i.e. their physical address). So instead of your is comparison:
You can do:
But, note that if a and b were directly in the comparison it would work.
In fact, in an expression there's sharing between the same static strings. But, at the program scale there's only sharing for word-like strings (so neither spaces nor punctuations).
You should not rely on this behavior as it's not documented anywhere and is a detail of implementation.
The
is
operator relies on theid
function, which isguaranteed to be unique among simultaneously existing objects.
Specifically,id
returns the object's memory address. It seems that CPython has consistent memory addresses for strings containing only characters a-z and A-Z.However, this seems to only be the case when the string has been assigned to a variable:
Here, the id of "foo" and the id of
a
are the same.a
has been set to "foo" prior to checking the id.However, the id of "bar" and the id of
a
are different when checking the id of "bar" prior to settinga
equal to "bar".Checking the id of "bar" again after setting
a
equal to "bar" returns the same id.So it seems that cPython keeps consistent memory addresses for strings containing only a-zA-Z when those strings are assigned to a variable. It's also entirely possible that this is version dependent: I'm running python 2.7.3 on a macbook. Others might get entirely different results.
'is' operator compare the actual object.
c is d
should also be false. My guess is that python make some optimization and in that case, it is the same object.To expand on Ignacio’s answer a bit: The
is
operator is the identity operator. It is used to compare object identity. If you construct two objects with the same contents, then it is usually not the case that the object identity yields true. 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. So theis
operator returns true for those.This however is an implementation detail of CPython and is generally neither guaranteed for CPython nor any other implementation. So using this fact is a bad idea as it can break any other day.
To compare strings, you use the
==
operator which compares the equality of objects. Two string objects are considered equal when they contain the same characters. So this is the correct operator to use when comparing strings, andis
should be generally avoided if you do not explicitely want object identity (example:a is False
).If you are really interested in the details, you can find the implementation of CPython’s strings here. But again: This is implementation detail, so you should never require this to work.