osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Explanation of list reference


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!

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".

That (I think) says everything is which is guaranteed about identities, 
and nothing more.  Once you've defined what an identity is, then you can 
go on to describe some fun things you can do with it:

"The id() function returns the identity of an object.  The 'is' operator 
compares the identities of its two operands and returns True if they are 
the same."