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

Steven D'Aprano steve at pearwood.info
Sun Mar 6 00:59:55 CET 2011


Aahz wrote:

> The operators ``is`` and ``is not`` compare whether two objects are
> really the same object (have the same memory location).  Immutable
> objects with the same value and type may be cached to the same object for
> efficiency.  For example, ``'spam' is 'spam'`` is either ``True`` or
> ``False`` depending on Python implementation.  Singleton objects
> (``True``, ``False``, ``None``) are always the same object.


Please remove the bit about the same memory location. That is an 
implementation detail which may be true for CPython, it may even be true 
for any sensible implementation of Python, but it's surely not a 
necessary condition for any language calling itself Python. I don't 
think it adds any clarity, since "same object" is a more intuitive 
concept than memory location (even non-programmers understand what it 
means to say that "my car" and "the car parked in the driveway" are the 
same object). And memory location is not something that Python the 
language exposes to the caller. Implementations with moving garbage 
collectors, such as PyPy and (I think) Jython, may move objects. All in 
all, I believe that Python's docs should stay as far away from any 
discussion of memory addresses as possible.

It might be obvious to English speakers that ``is`` and ``is not`` do 
the opposite, but as it stands above, the documentation suggests that 
they are two different ways of writing the same thing.

None, True and False are not the only singletons in Python, but the 
above gives the false impression of an exhaustive list.


The more I think about this the more I agree with Fred Drake that we 
should keep this simple. The documentation for the ``is`` operator is 
not the place for a discussion of implementation-specific optimizations.

     The operator ``is`` tests whether the two operands are the
     same object. ``is not`` tests that they are different objects.

If we have to mention singletons here, and I don't think we do, then a 
single example is enough:

     Singleton objects such as ``None`` are, by definition,
     always the same object.



-- 
Steven



More information about the Doc-SIG mailing list