[Doc-SIG] Does the "is" operator only matter for mutable object?
Fred Drake
fdrake at acm.org
Sat Mar 5 05:14:13 CET 2011
On Fri, Mar 4, 2011 at 10:13 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> The operators is and is not compare whether two objects are really the same
> object; this usually only matters for mutable objects like lists, or for
> testing against singletons like None.
I think having text about mutability here is confusing. Consider:
>>> x = []
>>> a = x,
>>> b = x,
Here, a and b are different immutable objects, though they'll always
be equal. "is" and "==" will produce different results; whether that
matters is context-dependent. "==" will always return the *same*
result as the list in x is mutated.
Further:
>>> c = [],
>>> d = [],
c and d are different objects, and their equality will vary as the
contained lists are mutated.
a, b, c and d are all immutable.
So let's not condition the explanation with vague hints about when
testing identity makes sense. I expect it will lead to more confusion
than it will avoid.
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
"A storm broke loose in my mind." --Albert Einstein
More information about the Doc-SIG
mailing list