Surprise using the 'is' operator

Antoon Pardon apardon at forel.vub.ac.be
Tue Sep 26 08:21:32 EDT 2006


On 2006-09-26, Fredrik Lundh <fredrik at pythonware.com> wrote:
> codefire wrote:
>
>> I was just trying to check if objects were the same (object), didn't
>> know Integers were a special case.
>
> they're not, really; "is" works the same way for all objects.
>
> when you ask for a new immutable object, a Python implementation may 
> always reuse an existing object, if it wants to.
>
> the CPython implementation does this for small integers, booleans, None, 
> empty tuples, certain strings, type objects, etc.  this also applies to 
> library code; for example, string methods often return a reference to 
> "self" if the method didn't actually change anything.

I find this a bit oddly worded. Now the "may always reuse" phrase
suggests this is not an obligation and I can certainly understand
that in the case of integers. But when you enumerate examples you
include None and Booleans, creating the suggestion these don't
have to be implemented as singletons either and that there can be
more than one None, True and False object. Now I probably just
misunderstand, but I'm wondering, is there somewhere in the language
reference that specifies these have to be singletons?

-- 
Antoon Pardon



More information about the Python-list mailing list