[Doc-SIG] Does the "is" operator only matter for mutable object?

INADA Naoki songofacandy at gmail.com
Sat Mar 5 06:30:51 CET 2011


If detailed description is needed, my suggesting is:

"""
The operators ``is`` and ``is not`` compare whether two objects are
really the same object.
Note that immutable objects having same value and type may be unified
for efficiency.
For example, ``'spam' is 'spam'`` is either ``True`` or ``False`` depending on
Python implementation.
Especially, singleton objects like ``True``, ``False``, ``None`` are always same
object when thier type and value is same.
"""

This description may have to been splitted to separate paragraph.

On Sat, Mar 5, 2011 at 1:14 PM, Fred Drake <fdrake at acm.org> wrote:
>
> 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
> _______________________________________________
> Doc-SIG maillist  -  Doc-SIG at python.org
> http://mail.python.org/mailman/listinfo/doc-sig



--
INADA Naoki  <songofacandy at gmail.com>


More information about the Doc-SIG mailing list