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

G

grocery_stocker

Given the following....

[cdalten@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 154303824154303824


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?
 
E

Emile van Sebille

grocery_stocker wrote:
It seems like id(list[<some value>]) == id(<some value>).

It might seem that way, but test with other than single-character
strings, eg lists like [7],[8],[9] and try again.

Emile
 
J

Josh Holland

It seems like id(list[<some value>]) == id(<some value>).

Only when certain immutable objects are involved. It is the
implementation's option to allow different immutable values to be the
same object (same id()). In CPython, this is used to cache strings that
look like identifiers and small integers. This has been discussed here
a lot; have a look at the archives.
 
G

grocery_stocker

grocery_stocker wrote:

<snip>


It seems like id(list[<some value>]) == id(<some value>).

It might seem that way, but test with other than single-character
strings, eg lists like [7],[8],[9] and try again.

I still get the same thing...

[cdalten@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.
a=[7]
print a[0] 7
id(a[0]) 165911912
id(7) 165911912
b=[8]
id(b[0]) 165911900
id(8) 165911900
c=[9]
id(c[0]) 165911888
id(9) 165911888
 
G

Gary Herron

grocery_stocker said:
Given the following....

[cdalten@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
154303848
154303836
154303824
for x in list:
... print id(x),
...
154303848 154303836 154303824
154303848

154303836
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

 
M

Martin v. Löwis

It seems like id(list[ said:
can't find anything in the python documentation that talks about it.

It's deliberately undocumented (outside of the source code, that is).

Regards,
Martin
 
E

Emile van Sebille

grocery_stocker said:
grocery_stocker said:
It seems like id(list[<some value>]) == id(<some value>).
It might seem that way, but test with other than single-character
strings, eg lists like [7],[8],[9] and try again.
I still get the same thing...

Well, yes -- because you're still testing the same things. I meant of
course to test other things, like lists as in:
>>> for ii in [[7],[7],[8],[8],[9],[9]]: id(ii)
....
16607992
16608952
16609032
16608992
16608832
16608872

What you're testing is the content of the list elements, more like:
>>> for ii in [[7],[7],[8],[8],[9],[9]]: id(ii[0])
....
15424016
15424016
15424004
15424004
15423992
15423992

And as you've heard, interning is at the root of the apparent sameness..

Emile
 
A

Aahz

[cdalten@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.

Don't use a variable named "list" -- you're hiding the list() type.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong." --GvR, python-ideas, 2009-3-1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top