list comprehension problem
Terry Reedy
tjreedy at udel.edu
Fri Oct 30 00:49:00 EDT 2009
alex23 wrote:
> On Oct 30, 1:10 pm, Nick Stinemates <n... at stinemates.org> wrote:
>>> Some objects are singletons, ie there's only ever one of them. The most
>>> common singleton is None. In virtually every other case you should be
>>> using "==" and "!=".
>> Please correct me if I am wrong, but I believe you meant to say some
>> objects are immutable, in which case you would be correct.
>
> You're completely wrong. Immutability has nothing to do with identity,
> which is what 'is' is testing for:
What immutability has to do with identity is that 'two' immutable
objects with the same value *may* actually be the same object,
*depending on the particular version of a particular implementation*.
>
>>>> t1 = (1,2,3) # an immutable object
>>>> t2 = (1,2,3) # another immutable object
Whether or not this is 'another' object or the same object is irrelevant
for all purposes except identity checking. It is completely up to the
interpreter.
>>>> t1 is t2
> False
In this case, but it could have been True.
>>>> t1 == t2
> True
>
> MRAB was refering to the singleton pattern[1], of which None is the
> predominant example in Python. None is _always_ None, as it's always
> the same object.
And in 3.x, the same is true of True and False.
More information about the Python-list
mailing list