Where's the documentation to support the following behavior...

Gary Herron gherron at islandtraining.com
Tue Mar 17 18:48:23 EDT 2009


grocery_stocker wrote:
> Given the following....
>
> [cdalten at localhost ~]$ python
> Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
> [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>   
>>>> list = [7,8,9]
>>>> id(list)
>>>>         
> -1209401076
>   
>>>> id(list[0])
>>>>         
> 154303848
>   
>>>> id(list[1])
>>>>         
> 154303836
>   
>>>> id(list[2])
>>>>         
> 154303824
>   
>>>> for x in list:
>>>>         
> ...    print id(x),
> ...
> 154303848 154303836 154303824
>   
>>>> id(7)
>>>>         
> 154303848
>   
>>>> id(8)
>>>>         
> 154303836
>   
>>>> id(9)
>>>>         
> 154303824
>
>
> It seems like id(list[<some value>]) == id(<some value>). However, I
> can't find anything in the python documentation that talks about it.
> Did I perhaps overlook something?
>   

No you didn't overlook anything.  Here's the (full and complete) 
documentation for id.    Anything else you may notice about the values 
of id() are implementation details, and must not be depended upon.    
You may, however, be able to use what you've noticed to discern some 
things about the inner workings of Python, in particular how it stores 
multiple instances of immutable objects.    But be warned Python makes 
some fairly complex decisions here in the interest of efficiency.

*id*( 	object)

    Return the ``identity'' of an object. This is an integer (or long
    integer) which is guaranteed to be unique and constant for this
    object during its lifetime. Two objects with non-overlapping
    lifetimes may have the same id() value. (Implementation note: this
    is the address of the object.) 

Gary Herron


> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list