Explanation of list reference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 15 20:05:03 CET 2014


On Sat, 15 Feb 2014 12:02:39 -0500, Roy Smith wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
>> > Object identity is simple and well-defined in Python. I don't know
>> > why you are so resistant to this. Read the documentation.
> 
> Marko Rauhamaa <marko at pacujo.net>:
>> It is not defined at all:
>> 
>>    Every object has an identity, a type and a value. An object’s
>>    identity never changes once it has been created; you may think of it
>>    as the object’s address in memory. The ‘is‘ operator compares the
>>    identity of two objects; the id() function returns an integer
>>    representing its identity.
> 
> The "you may think of it as the object's address in memory" part is
> misleading, and should be removed from the docs.  While it's true that
> you may think of it that way, such thinking just leads you to make
> assumptions which are not universally true.
> 
> I agree with Marko that this is not a definition.  It's a collection of
> random statements about ids, their use, and some misleading philosophy.
> Even the part about "... compares the identify of two objects" is kind
> of funky, since it implies that you do indeed have two objects!

Correct, it should say "two operands" to be pedantic.

But in regular English, it is quite normal to say things like "the two 
people actually turned out to be same person" (say, Superman and Clark 
Kent) and I see no reason not to allow the similar, technically sloppy 
but easily understandable idea that the `is` operator tests whether two 
objects are in fact the same object.

This concept isn't really that hard to understand. In the expression `a 
is b`, you have an object bound to the name "a", an object bound to the 
name "b", hence *two objects*, and you want to know if they are the same 
object or not.


> A definition would be:
> 
> "The identity of an object is an integer which never changes during the
> lifetime of the object, and which is guaranteed to be distinct from the
> identities of all other objects with overlapping lifetimes.  A given
> identity may be reused for objects with disjoint lifetimes".

You then go on to say:

> "The id() function returns the identity of an object. ..."


This definition is wrong. An object's identity and its ID are not the 
same. Objects in Python have an identity the instant they are created, 
but they may not have an ID assigned to them until much later, if at all. 
Both IronPython and Jython lazily assign IDs on request, not on creation. 
Here's an example from Jython:

>>> x = "first"
>>> y = "second"
>>> id(y)  # created second, but ID assigned first
1
>>> id(x)  # created first, but ID assigned second
2


IronPython is similar, although the specific IDs may not be the same.


The problem here is that you are wrongly identifying an object's identity 
with its identification number. That doesn't apply to people (if you are 
American, your identity is not the same as your Social Security number); 
it doesn't apply to rocks, or pencils, or kitchen spoons. Why should it 
apply to Python objects? The identity of an object is an inherent, 
fundamental aspect of the object's existence, not some label stuck to it.

In Jython, the `is` operator can distinguish objects without assigning 
them an ID number:

>>> a = 230000
>>> b = 230000
>>> a is b  # compare two ID-less objects for identity
False
>>> id("fe")
4
>>> id("fi")
5
>>> id(a)  # finally assign a an ID
6
>>> id("fo")
7
>>> id(b)  # and the same for b
8


In plain English, "identity" has various definitions, but the one needed 
here is this:

    The state or quality of being identical, or the same; sameness.
    [Webster 1913]

where "identical" is understood in the strong sense of:

    being the exact same one; not any other
    [Wordnet 2006]


rather than the weak sense of merely looking the same, as in "identical 
houses". These are common, ordinary words, and no more need specialist 
definitions in the Python documentation than do the other common, 
ordinary words used, such as "never", "changes" or "created".

An object's identity is that quality which distinguishes it from every 
other object. The nature of that quality is not important. Not 
withstanding such interesting but irrelevant philosophical questions as 
the Paradox Of My Grandfather's Axe, the intuitive, common, plain-English 
meaning of identity is all we need to understand object identity, because 
it's the same meaning.



-- 
Steven



More information about the Python-list mailing list